Home > Back-end >  search firbase document for a specific field in flutter dart and store it
search firbase document for a specific field in flutter dart and store it

Time:11-06

im trying to restore a document that its field "email" equal to certain value my code didnt get me anything dont know what is the problem

 void EditDisplayedName(String email, String name) async {
  CollectionReference s = FirebaseFirestore.instance
      .collection("Users")
      .doc("list_instructors")
      .collection("Instructor")
    ..where("Email", isEqualTo: email);

  s.doc(email).update({'Full Name': name});
} //end method

CodePudding user response:

Your code doesn't yet find/read the document for the email address.

The correct process is:

// 1. Create a reference to the collection
CollectionReference s = FirebaseFirestore.instance
    .collection("Users")
    .doc("list_instructors")
    .collection("Instructor")

// 2. Create a query for the user with the given email address
Query query = s.where("Email", isEqualTo: email);

// 3. Execute the query to get the documents
QuerySnapshot querySnapshot = await query.get();

// 4. Loop over the resulting document(s), since there may be multiple
querySnapshot.docs.forEach((doc) {

  // 5. Update the 'Full Name' field in this document
  doc.reference.update({'Full Name': name});
});

  • Related