Query<T extends dynamic> class
A query over the documents of a collection or a collection group, which you can read with get or listen to with snapshots.
A Query is immutable: where, orderBy, limit, limitToLast and the cursors (startAt, startAfter,
endAt, endBefore and their Document variants) each return a new query. Building a query does not contact the
backend; invalid combinations throw an ArgumentError when the method is called, and the backend may still
reject a query that needs an index it does not have.
Get a query from FirebaseFirestore.collection (a CollectionReference is a query over its documents) or
FirebaseFirestore.collectionGroup. T is the type of the documents, Map<String, Object?> unless a converter was
attached with withConverter.
Queries work offline: get and snapshots answer from the local cache when the backend cannot be reached.
final Query<Map<String, Object?>> bigCities = firestore
.collection('cities')
.where('population', isGreaterThan: 1000000)
.orderBy('population', descending: true)
.limit(10);
final QuerySnapshot<Map<String, Object?>> snapshot = await bigCities.get();
for (final QueryDocumentSnapshot<Map<String, Object?>> city in snapshot) {
print('${city.id}: ${city['population']}');
}
Subclassing Note: Firestore classes are not meant to be subclassed except for use in test mocks. Subclassing is not supported in production code and new SDK releases may break code that does so.
- Implementers
Properties
-
converter
→ FirestoreDataConverter<
T> ? -
The converter attached with withConverter, or null for raw maps.
final
- firestore → FirebaseFirestore
-
The FirebaseFirestore instance this query belongs to.
final
- hashCode → int
-
The hash code for this object.
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
aggregate(
List< AggregateField> aggregateFields) → AggregateQuery - Returns an AggregateQuery that calculates the given aggregations over the result set of this query without downloading the documents.
-
aggregateWithAliases(
Map< String, AggregateField> aggregates) → AggregateQuery -
Returns an AggregateQuery that calculates the given aggregations over the
result set of this query, with results keyed by user-chosen aliases, as
the JS SDK's
getAggregateFromServer(query, {total: sum('price')}): -
count(
) → AggregateQuery - Returns an AggregateQuery that counts the documents in the result set of this query, without downloading the documents.
-
endAt(
Iterable< Object?> fieldValues) → Query<T> - Creates and returns a new Query that ends at the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.
-
endAtDocument(
DocumentSnapshot< Object?> snapshot) → Query<T> - Creates and returns a new Query that ends at the provided document (inclusive). The end position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.
-
endBefore(
Iterable< Object?> fieldValues) → Query<T> - Creates and returns a new Query that ends before the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.
-
endBeforeDocument(
DocumentSnapshot< Object?> snapshot) → Query<T> - Creates and returns a new Query that ends before the provided document (exclusive). The end position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.
-
findNearest(
{required Object vectorField, required Object queryVector, required int limit, required DistanceMeasure distanceMeasure, String? distanceResultField, double? distanceThreshold}) → VectorQuery< T> -
Returns a VectorQuery that finds the
limitdocuments whosevectorField(a String or a FieldPath) is nearest toqueryVectoraccording todistanceMeasure. -
get(
[GetOptions? options]) → Future< QuerySnapshot< T> > - Executes the query and returns the results as a QuerySnapshot.
-
limit(
int limit) → Query< T> - Creates and returns a new Query that's additionally limited to only return up to the specified number of documents. This overrides a previous limitToLast.
-
limitToLast(
int limit) → Query< T> -
Creates and returns a new Query that only returns the last matching documents up to the specified number. This
overrides a previous
limit. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
orderBy(
Object field, {bool descending = false}) → Query< T> -
Creates and returns a new Query that's additionally sorted by
field, a String (dots separate nested fields; throws an ArgumentError for an invalid path) or a FieldPath (FieldPath.documentId sorts by document id), in ascending order or, withdescending, in descending order. -
snapshots(
{bool includeMetadataChanges = false, ListenSource source = ListenSource.defaultSource}) → Stream< QuerySnapshot< T> > - A stream of the results of this query: one QuerySnapshot now, and another each time the results change.
-
startAfter(
Iterable< Object?> fieldValues) → Query<T> - Creates and returns a new Query that starts after the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.
-
startAfterDocument(
DocumentSnapshot< Object?> snapshot) → Query<T> - Creates and returns a new Query that starts after the provided document (exclusive). The starting position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.
-
startAt(
Iterable< Object?> fieldValues) → Query<T> - Creates and returns a new Query that starts at the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.
-
startAtDocument(
DocumentSnapshot< Object?> snapshot) → Query<T> - Creates and returns a new Query that starts at the provided document (inclusive). The starting position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.
-
toString(
) → String -
A string representation of this object.
override
-
where(
Object fieldOrFilter, {Object? isEqualTo, Object? isNotEqualTo, Object? isLessThan, Object? isLessThanOrEqualTo, Object? isGreaterThan, Object? isGreaterThanOrEqualTo, Object? arrayContains, Iterable< Object?> ? arrayContainsAny, Iterable<Object?> ? whereIn, Iterable<Object?> ? whereNotIn, bool? isNull}) → Query<T> - Creates and returns a new Query with additional filters.
-
withConverter<
U extends Object?> ({required FromFirestore< U> fromFirestore, required ToFirestore<U> toFirestore}) → Query<U> -
Returns a query that converts documents with
fromFirestoreandtoFirestore(typeU). Filters, ordering and limits are kept. Use withoutConverter to go back to raw maps. -
withoutConverter(
) → Query< Map< String, Object?> > -
Returns a query over the same documents that yields raw
Map<String, Object?>values (the equivalent ofwithConverter(null)in the JS SDK).
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override