Home > front end >  How do I get the DocumentID of a firestore document that has a particular field value in flutter?
How do I get the DocumentID of a firestore document that has a particular field value in flutter?

Time:08-24

For example,

In the image below, I want to get the document ID that has 'banana' in its food field. How can I query all documents in 'store1', find the field that has 'banana', and then return that document ID that contains it?

enter image description here

CodePudding user response:

You can run a query and get the id in then

Firestore.instance
    .collection('Store1')
    .where("food", isEqualTo: "banana")
    .getDocuments()
    .then(
      (QuerySnapshot snapshot) => {
        snapshot.documents.forEach((item) {
          print("documentID is : "   item.reference.documentID);
        }),
      },
    );
  • Related