How to get Firestore Data by method in Flutter

Question

Asked by gurkan s on November 06, 2021 (source).

I am trying to get users name but Flutter gives this error:

The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.
Try adding either a return or a throw statement at the end.

Method:

  String getUserNameFromUID(String uid) {

    FirebaseFirestore.instance
        .collection('users')
        .where('uid', isEqualTo: uid)
        .get()
        .then((QuerySnapshot querySnapshot) {
      querySnapshot.docs.forEach((doc) {
        return doc["name"];
      });

    });

  }

How can I solve my problem? if I add return 0 to end of the method it always gives 0.

It always gives 0.(I do not want 0, I want get user name from uid)

  String getUserNameFromUID(String uid) {

    FirebaseFirestore.instance
        .collection('users')
        .where('uid', isEqualTo: uid)
        .get()
        .then((QuerySnapshot querySnapshot) {
      querySnapshot.docs.forEach((doc) {
        return doc["name"];
      });

    });

return "0";

  }

EDIT: I need a String solution, not Future. The method should return String...

Because my UI is not future builder. Isn't there any way to return one data as String in Firestore database?

Answer

Question answered by loki (source).

First your function should return a Future<String> since it relies on firestore's get wich also returns a future. Also docs is a list, you have to return just one. The first one i guess. In the UI just use a FutureBuilder

 Future<String> getUserNameFromUID(String uid) async {

    final snapshot = await FirebaseFirestore.instance
        .collection('users')
        .where('uid', isEqualTo: uid)
        .get();
     return snapshot.docs.first['name'];

  }

Since you can't use FutureBuilder. An ugly alternative is to pass a callback to getUserNameFromUID and call setState from there.

void getUserNameFromUID(String uid, Function (String name) onData) {

    final snapshot = await FirebaseFirestore.instance
        .collection('users')
        .where('uid', isEqualTo: uid)
        .get().then((s) => onData(s.docs.first['name']));
  }

On your UI

...
getUserNameFromUID(uid, (String name){
  setState(()=> name = name);
});

From your last comment just inherit from StatefulWidget. And call the function from inside.

@override
void initState() {
   getUserNameFromUID(uid);
}

If you had special requirements about not being able to modify the UI, you should mention that as it conditions the way to use the backend services.

DART FIREBASE FLUTTER GOOGLE-CLOUD-FIRESTORE
SHARE: