Here is your potential answer
There are 3 types of changes can happen for document
- New Doc added
- Doc updated
- 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