skip to Main Content

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.

2

Answers


  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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search