how to update data after collection in firebase firestore - flutter

Viewed 2277

Am trying to update my firestore database. It is a chat database where am trying to update the chat message if it is read or unread and this is my code

updateIsread(String receiverId, String chatRoomId) async {
    FirebaseFirestore.instance
        .collection("chatRoom")
        .doc(chatRoomId)
        .collection("chats")
        .where('sendTo', isEqualTo: receiverId);
  }

The problem is that after .collection("chats") i can't use .update() Please help me out and this is my firebase structure enter image description here

enter image description here

1 Answers

You can try to get a QuerySnapshot first, then from first document in it you can know DocumentReference and use update on it:

await QuerySnapshot _querySnapshot = FirebaseFirestore.instance
        .collection("chatRoom")
        .doc(chatRoomId)
        .collection("chats")
        .where('sendTo', isEqualTo: receiverId).get();
if (_querySnapshot.docs.isNotEmpty) await _querySnapshot.docs[0]
                                         .reference
                                         .update('YourUpdateData');

There might be a better way for doing this, but thats what worked for me.

Related