foreverLearnerDevs
foreverLearnerDevs

Reputation: 69

Get user data from Firebase

I'm trying to get user data from their uid and use it on text widget. Should I create getCurrentUser method to get the data? I have tried using querysnapshots but it did nothing. Here is the code :

            Expanded(
              child: Container(
                margin: EdgeInsets.only(top: 10.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('Name'),
                    Text('[email protected]'),
                    Text('+02112345678'),
                  ],
                ),
                  ),
                ),

Here is the update :

            Expanded(
              child: Container(
                margin: EdgeInsets.only(top: 10.0),
                child: FutureBuilder(
                  future: getData(),
                  builder: (context,
                      AsyncSnapshot<DocumentSnapshot> snapshot) {
                    if (snapshot.connectionState ==
                        ConnectionState.done) {
                      return Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(snapshot.data['Full Name']),
                          Text(snapshot.data['Email Address']),
                          Text(snapshot.data['Phone Number']),
                        ],
                      );
                    } else if (snapshot.connectionState ==
                        ConnectionState.none) {
                      return Text("No Data");
                    }
                    return CircularProgressIndicator();
                  },
                ),
              ),

The output was like this :

enter image description here

enter image description here

Upvotes: 0

Views: 98

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

You need to use the FutureBuilder widget, first create a method to get the user:

  Future<DocumentSnapshot> getData() async {
    FirebaseUser firebaseUser = await FirebaseAuth.instance.currentUser();
    return await Firestore.instance
        .collection("UserAccount").document(firebaseUser.uid).get();
  }

Then inside the build method:

          FutureBuilder(
            future: getData(),
            builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                  return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(snapshot.data["name"]),
                    Text(snapshot.data["email"]),
                    Text(snapshot.data["number"]),
                  ],
                ),
              } else if (snapshot.connectionState == ConnectionState.none) {
                return Text("No data");
              }
              return CircularProgressIndicator();
            },
          ),

In the above code, we use a FutureBuilder since get() is asynchronous, then inside ConnectionState.done you will get the data using snapshot.data.


It's better if you read the documentation, check all the flutter cookbook:

https://flutter.dev/docs/cookbook

Upvotes: 1

Related Questions