Siddharth
Siddharth

Reputation: 153

FetchData From Cloud FireStore Firebase in flutter (dart)

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!

Upvotes: 0

Views: 519

Answers (1)

dkap
dkap

Reputation: 237

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");
      },
    );
  }
}

Upvotes: 1

Related Questions