0

This is my Fire Base cloud store structure the data is stored in the document is as Map<string,Dynamic>

enter image description here

What Would be the Query if i want to fetch the username to corresponding uid ?

Also , i want return the username to a text widget

Language Used : Dart

    String getUserName (User user) {
        String username;
        
        /*   Query  */
        
        return username;
      }

class username extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text("USER NAME : " + getUserName());
  }
}

Please Help!

1 Answer 1

1

You can use the FlutterFire Package to read Data from your Firestore https://firebase.flutter.dev/docs/firestore/usage/#one-time-read

Take a look at their example, you only have to make a few Adjustments:

    class GetUserName extends StatelessWidget {
  final String documentId; // This is your User UID

  GetUserName(this.documentId);

  @override
  Widget build(BuildContext context) {
    CollectionReference users = FirebaseFirestore.instance.collection('users');

    return FutureBuilder<DocumentSnapshot>(
      future: users.doc(documentId).get(),
      builder:
          (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {

        if (snapshot.hasError) {
          return Text("Something went wrong");
        }

        if (snapshot.connectionState == ConnectionState.done) {
          Map<String, dynamic> data = snapshot.data.data();
          return Text("USER NAME: ${data['name']}");
        }

        return Text("loading");
      },
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.