Home > front end >  How do I get a Document ID If I only had a doucment field available?
How do I get a Document ID If I only had a doucment field available?

Time:12-01

I am working on a flutter project where I want a function that returns the Document ID. The only thing availabe is document field that is email or name.

Firestore Database.

What I want is:

if (email == email) {
  return documentID;
}

CodePudding user response:

new Promise((resolve, reject)=>{
  db.collection('users').get().then((snapShot)=>{
    const ids = [];
    snapShot.forEach((doc) => {
      const data = doc.data();
      if (data.email || data.name) {
        ids.push(doc.id);
      }
    });
    resolve(ids);
  });
});

CodePudding user response:

As much as I don't understand your request, I'll make a guess.

I guess you want to get the id of this document when it matches the value you want.

String? id;
await FirebaseFirestore.instance.collection('users').where('email', isEqualTo: 'email').get().then((value) => id = value.docs.first.id);

print(id); //DocumentID
  • Related