Context
I have a method of listening to the changes in a collection. It works correctly for an updated document. I am now trying to implement the deleting of the item on my UI when the document is deleted on Firestore. To do so I noticed the "exists" property of the DocumentSnapshot class which seems to be the information that I look for.
Expected result
I delete a document. Then I get notified and the "exists" property of the snapshot is false. So I can remove the item on my UI.
Actual result
I delete a document. Then I get notified but the "exists" property of the snapshot is true.
Am I misunderstanding the "exists" property or is it a bug?
EDIT : Sample of my code
Service:
static Future<void> getPoles(LatLng center, double zoom, Function(Stream<Pole>) callback) async {
// ...
for (var direction in directions) {
print("DIRECTION: $direction : ${_getEndcode(direction)}");
var snapshots = Firestore.instance.collection(POLES_COLLECTION)
.where(COL_POSITION_HASH, isGreaterThanOrEqualTo: direction, isLessThan: _getEndcode(direction))
.snapshots();
snapshots.listen((snapshot) {
var poles = _onPoleChanges(snapshot);
for (Pole pole in poles) {
polesController.add(pole);
}
});
}
callback(polesController.stream);
});
}
static List<Pole> _onPoleChanges(snapshot) {
List<Pole> poles = [];
print("Length: ${snapshot.documentChanges.length}");
for(var change in snapshot.documentChanges) {
DocumentSnapshot doc = change.document;
GeoPoint point = doc.data[PoleService.COL_POSITION]['geopoint'];
var states = doc.data[PoleService.COL_STATE];
if (states == null) states = [];
Pole pole = Pole(
exists: doc.exists,
id: doc.documentID,
note: doc.data[PoleService.COL_NOTE],
street: doc.data[PoleService.COL_STREET],
npa: doc.data[PoleService.COL_NPA],
locality: doc.data[PoleService.COL_LOCALITY],
geopoint: LatLng(point.latitude, point.longitude),
geohash: doc.data[PoleService.COL_POSITION]['geohash'],
withCharges: doc.data[PoleService.COL_WITHCHARGES],
states: List.from(states),
disabled: doc.data[PoleService.COL_DISABLED]
);
poles.add(pole);
}
return poles;
}
The object "pole" is given to the callback and give it to the UI. The UI listen on the Stream and call the following function when a change is detected:
void _onPoleChanges(Pole pole) {
if (!pole.exists) {
_markers.remove(MarkerId(pole.id));
return;
}
// ...
}