How to retrieve specific user details from firestore with flutter
5,388
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);
});
}
Author by
Mr. Balboa
Updated on December 08, 2022Comments
-
Mr. Balboa over 1 year
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.
-
Mr. Balboa over 5 yearsadding an await brings an error on userDetails message is - the expression here has a type of void and cannot be used.
-
Mr. Balboa over 5 yearsSorry, Still getting the same error. Is it because I'm declaring the userDetails as final? or because it is a future void method?
-
diegoveloper over 5 yearsI think you will need another future builder to get the userDetails, because it is async
-
Mr. Balboa over 5 yearsThanks, a nested future builder did it.