1
votes

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;
    }
  // ...
}
1
add the code you are usingPeter Haddad
Please add the code to your issue. Also a question. Have you enabled Persistence for FirestoreFardeen Khan
I think so. It is enabled by default, according to the documentation (firebase.google.com/docs/firestore/manage-data/enable-offline). Should I use the property which tell me if it's coming from the cache instead of the "exists" property ?LawCha
Please take a look at this and give it a try as it seems a potential solution. What's happening is that most likely the document is cached and thus you can still read it until the cached version is gone. So you either need to distinguish between the online and offline version, or clear the cached documents whenever needed.Waelmas

1 Answers

0
votes

The solution is to use the documentSnapshot.metadata.isFromCache property if the persistance is enabled. Thanks to Fardeen Khan who helped to figure out in the comments.

EDIT: Well I had a case where it worked but not always. That is weird and so my problem is not completely resolved.

EDIT 2: I have to use the type property of DocumentChange class. It works perfectly now! Source: https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots