Home > Software design >  Get Document ID from Firebase Query using Flutter
Get Document ID from Firebase Query using Flutter

Time:02-22

If I have a collection called userNames and I use a firebase query to search for a document that a database field uid matches the current user ID, how do I print the document ID of the documents that contain that term? enter image description here

CodePudding user response:

A query can return multiple documents that match the given condition and returns a QuerySnapshot (that contains zero or many DocumentSnapshots).

FirebaseFirestore.instance
  .collection('userNames')
  .where('uid', isEqualTo: FirebaseAuth.instance.currentUser!.uid)
  .get()
  .then((value) {
    value.docs.forEach((element) {
      print(element.id);
    });
  });

Here value.docs is a list of all the documents included in this snapshot and each of them has id property (the document ID).

  • Related