Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve specific user details from firestore with flutter

I'm new to flutter and firebase so bear with me. I'm using email sign up with firestore and flutter on my app, on registration some additional fields are saved to firestore. I want to retrieve those fields to display on the user profile.

The key identifier for the fields saved to the users collection is the auto generated user id upon sign up.

I have in my widget build context

child: new FutureBuilder<FirebaseUser>(
        future: _firebaseAuth.currentUser(),
        builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            String userID = snapshot.data.uid;
            _userDetails(userID);
            return new Text(firstName);
          }
          else {
            return new Text('Loading...');
          }
        },
      ),

And my get associated data method is:

Future<void> getData(userID) async {
// return await     Firestore.instance.collection('users').document(userID).get();
DocumentSnapshot result = await     Firestore.instance.collection('users').document(userID).get();
return result;

}

To retrieve the user details

void _userDetails(userID) async {
final userDetails = getData(userID);
            setState(() {
                  firstName =  userDetails.toString();
                  new Text(firstName);
});

}

I have tried adding a .then() to the set state in _userdetails but its saying userDetails is a type of void and cannot be assigned to string. The current code block here returns instance of 'Future' instead of the user Details.

like image 315
Mr. Balboa Avatar asked Oct 17 '22 09:10

Mr. Balboa


1 Answers

Your method is marked as async so you have to wait for the result :

        Future<void> _userDetails(userID) async {
        final userDetails = await getData(userID);
                    setState(() {
                          firstName =  userDetails.toString();
                          new Text(firstName);
        });
        }
like image 122
diegoveloper Avatar answered Oct 20 '22 06:10

diegoveloper