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);
        });
        }
Share:
5,388
Mr. Balboa
Author by

Mr. Balboa

Updated on December 08, 2022

Comments

  • Mr. Balboa
    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
    Mr. Balboa over 5 years
    adding an await brings an error on userDetails message is - the expression here has a type of void and cannot be used.
  • Mr. Balboa
    Mr. Balboa over 5 years
    Sorry, Still getting the same error. Is it because I'm declaring the userDetails as final? or because it is a future void method?
  • diegoveloper
    diegoveloper over 5 years
    I think you will need another future builder to get the userDetails, because it is async
  • Mr. Balboa
    Mr. Balboa over 5 years
    Thanks, a nested future builder did it.