MobDev
MobDev

Reputation: 33

Update firestore field from flutter with multiple documents

DB

I want to update the isSeen field to 1, filtered by idTo messages in flutter code.

void onSeenMessages(){

    Stream<QuerySnapshot> messageRef = Firestore.instance
        .collection("messages")
        .document(groupChatId)
        .collection(groupChatId)
        .snapshots();

    messageRef. {
      field.documents. {
        productName.add(field.documents[index]["name"]);
      });
    });

}

Upvotes: 0

Views: 1249

Answers (2)

MobDev
MobDev

Reputation: 33

Yes documentID is unknown and I need to check all the documents. here is the one I found and working fine now, let me know if this can be optimized.

void onSeenMessages() async{
    CollectionReference ref = Firestore.instance
        .collection('messages')
        .document(groupChatId)
        .collection(groupChatId);

    QuerySnapshot eventsQuery =  await ref.where('idTo', isEqualTo: id).where('isSeen', isEqualTo: 0).getDocuments();

    eventsQuery.documents.forEach((msgDoc) {
      msgDoc.reference.updateData({'isSeen': 1});
    });
  }

Upvotes: 3

Hagar2M
Hagar2M

Reputation: 241

As I understand, you need to change (isSeen) field:

You can do this:

Firestore.instance.collection('messages').document(groupChatId).collection(groupChatId).document(documentID).updateData({
            'isSeen ': 1,
      });

witch documentID as in this image:

enter image description here

If you don't know how to get documentID ? Please tell me.

Upvotes: 1

Related Questions