get method
Executes the query and returns the results as a QuerySnapshot.
By default, get() attempts to provide up-to-date data when possible by waiting for data from the server, but it may return cached data or fail if you are offline and the server cannot be reached. This behavior can be altered via the Source parameter.
GetOptions.source a value to configure the get behavior:
- Source.serverAndCache (the default): waits for the backend; when the client is offline it answers from the
cache, with
metadata.isFromCachetrue. - Source.server: fails with FirestoreErrorCode.unavailable when the backend cannot be reached.
- Source.cache: answers from the cache only, with the documents the cache has (possibly none).
Returns a Future that will be resolved with the results of the Query. Writes that the backend has not
acknowledged yet are included in the results. This is the JS SDK getDocs, getDocsFromServer and
getDocsFromCache.
final QuerySnapshot<Map<String, Object?>> snapshot = await firestore
.collection('cities')
.where('state', isEqualTo: 'CA')
.get();
for (final QueryDocumentSnapshot<Map<String, Object?>> city in snapshot.docs) {
print('${city.id} => ${city.data()}');
}
final QuerySnapshot<Map<String, Object?>> cached = await firestore
.collection('cities')
.get(const GetOptions(source: Source.cache));
Implementation
Future<QuerySnapshot<T>> get([GetOptions? options]) async {
final Source source = options?.source ?? Source.serverAndCache;
_validateHasExplicitOrderByForLimitToLast();
if (source == Source.cache) {
final ViewSnapshot viewSnap = await firestore.client
.getDocumentsFromLocalCache(query);
return QuerySnapshotInternal.create<T>(
Query<T>._(query, firestore, converter),
viewSnap,
firestore,
);
} else {
return _getViaSnapshotListener(source);
}
}