0

This is the screenshot of my database.

I want to fetch the record for a particular username. (For eg: where [email protected]) Can anyone suggest to me how to fetch this in flutter?

enter image description here

It would be a great help, Thank you.

1

4 Answers 4

2

To get the data, you can create a function like:

Future getData(String username) async {
    List dataList = [];
    try {
      await FirebaseFirestore.instance.collection('userdata').where('user', isEqualTo: username).get().then((QuerySnapshot querySnapshot) => {
            querySnapshot.docs.forEach((doc) {
              itemList.add(doc.data());
            }),
          });
      return itemList;
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

When you'll call this function, you will have to pass the username and it would return a list of data items.

This list can then be used to show data in the UI as:

 child: Text(
            title: Text(subjectList[index]['user']),
           ),
Sign up to request clarification or add additional context in comments.

1 Comment

Please refer the FlutterFire - Cloud Firestore usage documentation here: firebase.flutter.dev/docs/firestore/usage
1

you can try this approach it helped me, but if you have migrated to null safety just make sure that you change the code accordingly

Comments

1

You can try the following query to get your result.

FirebaseFirestore.instance
  .collection('your-collection-name')
  .where('user', arrayContains: '[email protected]')
  .get();

6 Comments

Future<QuerySnapshot> user = FirebaseFirestore.instance .collection('userdata') .where('user', arrayContains: email) .get(); This helps, but also can you tell me how to print my data using user?
If you want to get/ print all the user key values from the firebase collection. You can simple use the same query without the condition statement arrayContains: email. Future<QuerySnapshot> user = FirebaseFirestore.instance .collection('userdata') .where('user').get();
How to get a particular key-value pair? Suppose, I want to get the weight of the user, how to do that?
Hi @MahishaPatel, In Firebase we store the values in map type. So, If you want to get the weight of a user, you should pass the key (weight) into the where method. Like FirebaseFirestore.instance .collection('userdata') .where('weight').get();
When I print user, I just get this : Instance of 'Future<QuerySnapshot<Map<String, dynamic>>>'
|
0

For android solution follow this:

 FirebaseFirestore.getInstance()
                .collection("users").whereArrayContains("user","[email protected]").get();

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.