snapshotsInSync method
A stream that emits whenever all the snapshot listeners of this instance are in sync with each other, which means every listener received the snapshot that reflects the same state of the local cache and of the backend.
It emits once when it is listened to, and then once after each round of snapshots (it emits once for several snapshots that are raised together, also when none of the queries changed but only their metadata). It does not emit for changes that no snapshot listener is interested in.
Each call returns a new single-subscription stream; it does not replay earlier events. On a terminated instance the stream fails with the FirestoreException of a terminated instance and closes.
final StreamSubscription<void> subscription = firestore.snapshotsInSync()
.listen((_) => print('every listener is up to date'));
// ...
await subscription.cancel();
Implementation
Stream<void> snapshotsInSync() {
late final StreamController<void> controller;
void listener() {
if (!controller.isClosed) {
controller.add(null);
}
}
controller = StreamController<void>(
// A terminated instance fails the listen: report it on the stream.
onListen: () => client.addSnapshotsInSyncListener(listener).catchError((
Object error,
StackTrace stackTrace,
) {
if (!controller.isClosed) {
controller
..addError(error, stackTrace)
..close();
}
}),
onCancel: () => client.removeSnapshotsInSyncListener(listener),
);
return controller.stream;
}