get method

Future<QuerySnapshot<T>> get([
  1. GetOptions? options
])

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:

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);
  }
}