0

I have used below code to listen Firestore collection change,

firestoreInstance
    .collection('Countries')
    .snapshots()
    .listen((result) async {

});

When a document change in Countries collection on Firestore. I expect result value should have only changed document. But result value has all the collection data.

How to get only changed document in result value.

Im using Flutter Firestore.

praveenb
  • 10,549
  • 14
  • 61
  • 83
  • 1
    What you're asking for is not a feature of the Firestore API. See my answer here: https://stackoverflow.com/questions/66610587/listen-only-to-changes-in-document-instead-of-all-the-time and Doug's here: https://stackoverflow.com/questions/62314708/firestore-get-only-the-changed-documents-in-a-large-synced-collection – Frank van Puffelen Jun 02 '23 at 00:48

2 Answers2

1

Here is your potential answer There are 3 types of changes can happen for document

  1. New Doc added
  2. Doc updated
  3. Doc deleted

Here is example how to handle all of them :

firestoreInstance
    .collection('Countries')
    .snapshots()
    .listen((QuerySnapshot snapshot) {
  snapshot.docChanges.forEach((DocumentChange change) {
    if (change.type == DocumentChangeType.added) {
      // Handle added document
      print("Added document: ${change.doc.data()}");
    } else if (change.type == DocumentChangeType.modified) {
      // Handle modified document
      print("Modified document: ${change.doc.data()}");
    } else if (change.type == DocumentChangeType.removed) {
      // Handle removed document
      print("Removed document: ${change.doc.data()}");
    }
  });
});

Another way is if you want to check for updated docs only

firestoreInstance
    .collection('Countries')
    .snapshots()
    .listen((QuerySnapshot snapshot) {
  var modifiedDocuments = snapshot.docChanges
      .where((change) => change.type == DocumentChangeType.modified)
      .map((change) => change.doc)
      .toList();

  // Process the modified documents
  modifiedDocuments.forEach((modifiedDocument) {
    // Do something with the modified document
    print('Modified document: ${modifiedDocument.data()}');
  });
});

Here is the reference for that

https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentChange

Rohan Jariwala
  • 1,564
  • 2
  • 7
  • 24
  • I donot want to iterate each snapshot and check document status. I want to get only updated doc snapshot. But result value has all the collection data. – praveenb Jun 01 '23 at 18:37
  • Sorry for that but Firebase does not provide a direct mechanism to listen for only updated documents. As there can be multiple updated document at once. – Rohan Jariwala Jun 01 '23 at 19:21
  • I've updated my answer with another option if you want to use that one – Rohan Jariwala Jun 01 '23 at 19:26
1

For Flutter/Dart Firestore, you can know only changed document. Here's an example in Flutter/Dart that demonstrates how to filter out only the changed documents.

  firestoreInstance
        .collection('Countries')
        .snapshots()
        .listen((event) {
      for (var change in event.docChanges) {
        switch (change.type) {
          case DocumentChangeType.added:
            print("New Countries: ${change.doc.data()}");
            break;
          case DocumentChangeType.modified:
            print("Modified Countries: ${change.doc.data()}");
            break;
          case DocumentChangeType.removed:
            print("Removed Countries: ${change.doc.data()}");
            break;
        }
      }
    });

Here is official documentation link https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots

Karamat Subhani
  • 104
  • 1
  • 7