snapshots method
- bool includeMetadataChanges = false,
- ListenSource source = ListenSource.defaultSource,
A stream of the results of this query: one QuerySnapshot now, and another each time the results change.
The stream listens to the backend while it has a subscriber; cancel the subscription to stop, and always cancel it when you no longer need it. Each call returns a new single-subscription stream. While the client is offline the stream keeps delivering changes from the cache. An error (for example a query that the security rules deny, or a missing index) is delivered on the stream, which then closes. Throws a StateError for a limitToLast query without orderBy.
With includeMetadataChanges a snapshot also arrives when only the QuerySnapshot.metadata changed (for
example when the results stop being from the cache, or when a local write is acknowledged), and
QuerySnapshot.docChanges lists those documents too. With ListenSource.cache as source the events come
from the local cache only (local writes, cache updates) and the backend is not contacted for this listener.
This is the JS SDK onSnapshot.
final StreamSubscription<QuerySnapshot<Map<String, Object?>>> subscription =
firestore
.collection('cities')
.where('state', isEqualTo: 'CA')
.snapshots()
.listen(
(QuerySnapshot<Map<String, Object?>> snapshot) {
for (final DocumentChange<Map<String, Object?>> change
in snapshot.docChanges) {
print('${change.type.name}: ${change.doc.id}');
}
},
onError: (Object error) => print('listen failed: $error'),
);
// ...
await subscription.cancel();
Implementation
Stream<QuerySnapshot<T>> snapshots({
bool includeMetadataChanges = false,
ListenSource source = ListenSource.defaultSource,
}) {
return _getSnapshotsInternal(
_internalOptions(includeMetadataChanges, source),
);
}