Home > Enterprise >  Updating a field in an unknown document id firebase\flutter
Updating a field in an unknown document id firebase\flutter

Time:10-24

I have a problem with updating a field without knowing the document id. in my app I have a collection name myPost which holds the users post, I want to edit the review of the user so I used where query to reach the document and I reached it but I didn't know how to update its fields. this the edit method I tried to wrote, if anyone can help I'd be thankful.

void editReview({required String newRev, required String oldRev}) {
emit(EditReviewloaded());

        FirebaseFirestore.instance
            .collection('Users')
            .doc(uId)
            .collection("MyPost")
            .where('review', isEqualTo: oldRev)
            .get()
            .then((value) => value.docs.forEach((element) {
                  element.data()['review'] = newRev;  //not working
                  
                  emit(EditReviewdone());
                }));
      }

CodePudding user response:

You can get id of document with element.id

FirebaseFirestore.instance
.collection('Users')
.doc(uId)
.collection("MyPost")
.where('review', isEqualTo: oldRev)
.get()
.then((value) => value.docs.forEach((element) {
element.id                
}));

and update it with this method

FirebaseFirestore.instance
.collection('Users')
.doc(uId)
.collection("MyPost")
.doc(docPostId).update({'review': newRev});
  • Related