0

I have created function get user and set it's data from firestore, this is the code of function getUser.

  Future<User> getUser(String uid) async{
    User user;
    _firestore
        .collection(USERS_COLLECTION)
        .where("uid", isEqualTo: uid.toString())
        .getDocuments()
        .then((doc) {
      _firestore
          .document('/$USERS_COLLECTION/${doc.documents[0].documentID}')
          .get()
          .then((userData) {

        user = User(
          name: userData.data["name"],
          username: userData.data["username"],
          profilePhoto: userData.data["profilePic"],
        );


      }).catchError((e) {
        print(e);
      });
    });

    return user;

  }

Then I have my profile page I have created function to set user from getUser() to current user like this:

User me;
String myUID = "t4skPFRXcLPxAWvhHpaiPOfsrPI3";

    @override
      void initState() {
        super.initState();
        setUser();
      }
         ......

Future<void> setUser() async{
    me = await userManagment.getUser(myUID);
  }

But when I try to use print for example print(me.name) does not anything happen, when I try to set url of networkImage to me.profilePhoto there is an error showing tell me the url it's null.

2
  • Does this answer your question? What is a Future and how do I use it? Commented Feb 2, 2021 at 18:07
  • @ChristopherMoore I have done everything like all instructions inside your link but If there are things should I do tell me or something I done it in wrong way tell me. Commented Feb 2, 2021 at 18:14

1 Answer 1

1

Don't mix async-await and .then syntax. It's something that can be done, but it will more likely confuse than help. Adding the async modifier to your function is doing nothing since your function does not use await.

Consider the following options:

With .then

Future<User> getUser(String uid) {
    return _firestore
        .collection(USERS_COLLECTION)
        .where("uid", isEqualTo: uid.toString())
        .getDocuments()
        .then((doc) {
      return _firestore
          .document('/$USERS_COLLECTION/${doc.documents[0].documentID}')
          .get()
          .then((userData) {

        return User(
          name: userData.data["name"],
          username: userData.data["username"],
          profilePhoto: userData.data["profilePic"],
        );


      }).catchError((e) {
        print(e);
      });
    });
  }

With async-await

Future<User> getUser(String uid) async{
    User user;

    try{
    var doc = await _firestore
        .collection(USERS_COLLECTION)
        .where("uid", isEqualTo: uid.toString())
        .getDocuments();
    
    var userData = await _firestore
          .document('/$USERS_COLLECTION/${doc.documents[0].documentID}')
          .get();

    user = User(
          name: userData.data["name"],
          username: userData.data["username"],
          profilePhoto: userData.data["profilePic"],
        );
    }
    catch(e) {
      print(e);
    }
    return user;

  }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.