CollectionReference<T extends dynamic> class

A reference to a collection, used to add documents, get references to documents, and query the documents (using the methods inherited from Query).

Get one from FirebaseFirestore.collection or DocumentReference.collection. A collection exists as long as it has documents: creating a reference does not contact the backend, and a collection does not need to be created before you add a document to it.

final CollectionReference<Map<String, Object?>> cities = firestore
    .collection('cities');

// A new document with a generated id.
final DocumentReference<Map<String, Object?>> tokyo = await cities.add({
  'name': 'Tokyo',
  'country': 'Japan',
});
print(tokyo.id);

// A document with a chosen id.
await cities.doc('SF').set({'name': 'San Francisco'});

// A query over the collection.
final QuerySnapshot<Map<String, Object?>> japanese = await cities
    .where('country', isEqualTo: 'Japan')
    .get();
print(japanese.length);

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.

Inheritance

Properties

converter → FirestoreDataConverter<T>?
The converter attached with withConverter, or null for raw maps.
finalinherited
firestore → FirebaseFirestore
The FirebaseFirestore instance this query belongs to.
finalinherited
hashCode → int
The hash code for this object.
no setterinherited
id → String
The id of the collection: the last segment of its path.
no setter
parent → DocumentReference<Map<String, Object?>>?
Gets a DocumentReference to the document that contains this collection. Only subcollections are contained in a document. For root collections, returns null.
no setter
path → String
Gets the path of this collection (relative to the root of the database) as a slash-separated string.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

add(T data) → Future<DocumentReference<T>>
Adds a new document to this collection with the specified data, assigning it a document ID automatically.
aggregate(List<AggregateField> aggregateFields) → AggregateQuery
Returns an AggregateQuery that calculates the given aggregations over the result set of this query without downloading the documents.
inherited
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')}):
inherited
count() → AggregateQuery
Returns an AggregateQuery that counts the documents in the result set of this query, without downloading the documents.
inherited
doc([String? documentPath]) → DocumentReference<T>
Gets a DocumentReference instance that refers to the document at the specified path within this collection.
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.
inherited
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.
inherited
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.
inherited
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.
inherited
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 limit documents whose vectorField (a String or a FieldPath) is nearest to queryVector according to distanceMeasure.
inherited
get([GetOptions? options]) → Future<QuerySnapshot<T>>
Executes the query and returns the results as a QuerySnapshot.
inherited
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.
inherited
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.
inherited
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, with descending, in descending order.
inherited
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.
inherited
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.
inherited
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.
inherited
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.
inherited
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.
inherited
toString() → String
A string representation of this object.
inherited
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.
inherited
withConverter<U extends Object?>({required FromFirestore<U> fromFirestore, required ToFirestore<U> toFirestore}) → CollectionReference<U>
Returns a collection reference to the same collection that converts data with fromFirestore and toFirestore (type U) when reading and writing. Use withoutConverter to go back to raw maps.
override
withoutConverter() → CollectionReference<Map<String, Object?>>
Returns a collection reference to the same collection that reads and writes raw Map<String, Object?> values.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited