getQuery<T extends Object?> method

Future<QuerySnapshot<T>> getQuery<T extends Object?>(
  1. Query<T> query
)

Runs query on the server inside this transaction and returns its results. Only in a read-only transaction (runTransaction(..., readOnly: true)): the documents are those of the snapshot of the database that every read of the transaction sees.

final QuerySnapshot<Map<String, Object?>> cities =
    await firestore.runTransaction(
      (Transaction transaction) => transaction.getQuery(
        firestore.collection('cities').where('capital', isEqualTo: true),
      ),
      readOnly: true,
    );

In a read-write transaction the returned Future fails with a FirestoreException with code invalidArgument: such a transaction verifies at commit that the documents it read did not change, which is not possible for the result of a query. It fails with an ArgumentError if query belongs to another FirebaseFirestore instance.

The snapshot never comes from the cache and has no pending writes; local writes that the server has not seen are not part of it.

Not in the JS client SDK (its server SDKs have queries in transactions).

Implementation

Future<QuerySnapshot<T>> getQuery<T extends Object?>(Query<T> query) async {
  _validateQuery(query);
  final List<Document> documents = await _transaction.runQuery(query.query);
  DocumentSet documentSet = DocumentSet.emptySet(query.query.comparator);
  for (final Document document in documents) {
    documentSet = documentSet.add(document);
  }
  return QuerySnapshotInternal.create<T>(
    query,
    ViewSnapshot.fromInitialDocuments(
      query.query,
      documentSet,
      DocumentKey.emptyKeySet,
      isFromCache: false,
      excludesMetadataChanges: false,
      hasCachedResults: false,
    ),
    _firestore,
  );
}