DocumentReference<T extends dynamic> class

A DocumentReference refers to a document location in a Firestore database and can be used to write, read, or listen to the location. There may or may not exist a document at the referenced location. A DocumentReference can also be used to create a CollectionReference to a subcollection.

Get one from FirebaseFirestore.doc or CollectionReference.doc. Creating a reference does not contact the backend. T is the type of the document data, Map<String, Object?> unless a converter was attached with withConverter. Two references are equal when they point to the same document of the same FirebaseFirestore instance with the same converter.

Writes (set, update, delete) are applied to the local cache at once and their futures complete when the backend has acknowledged them; while the client is offline the futures do not complete, and the writes are sent when it is back online.

final DocumentReference<Map<String, Object?>> sf = firestore.doc(
  'cities/SF',
);
await sf.set({'name': 'San Francisco', 'population': 870000});
await sf.update({'population': FieldValue.increment(1)});

final DocumentSnapshot<Map<String, Object?>> snapshot = await sf.get();
print(snapshot['population']);

await sf.delete();

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.

Constructors

DocumentReference.fromJson(FirebaseFirestore firestore, Map<String, Object?> json, {FirestoreDataConverter<T>? converter})
Builds a DocumentReference of firestore from a JSON map created by toJson, optionally with a converter.
factory

Properties

converter → FirestoreDataConverter<T>?
The converter attached with withConverter, or null for raw maps.
final
firestore → FirebaseFirestore
The Firestore instance this reference belongs to. A WriteBatch or a Transaction only accepts references of its own instance.
final
hashCode → int
The hash code for this object.
no setteroverride
id → String
The id of the document: the last segment of its path.
no setter
parent → CollectionReference<T>
Gets a CollectionReference to the collection that contains this document.
no setter
path → String
Gets the path of this document (relative to the root of the database) as a slash-separated string, such as cities/SF.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

collection(String collectionPath) → CollectionReference<Map<String, Object?>>
Gets a CollectionReference instance that refers to the subcollection at the specified path relative to this document.
delete() → Future<void>
Deletes the document referred to by this DocumentReference.
get([GetOptions? options]) → Future<DocumentSnapshot<T>>
Reads the document referenced by this DocumentReference.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
set(T data, [SetOptions? options]) → Future<void>
Writes to the document referred to by this DocumentReference. If the document does not yet exist, it will be created. If you pass SetOptions, the provided data can be merged into an existing document.
snapshots({bool includeMetadataChanges = false, ListenSource source = ListenSource.defaultSource}) → Stream<DocumentSnapshot<T>>
A stream of snapshots of this document: the current contents first, then a new snapshot every time the contents change.
toJson() → Map<String, Object>
Returns a JSON-serializable representation of this reference: its type (jsonType) and referencePath, like the JS SDK's DocumentReference.toJSON(). The Firestore instance and the converter are not part of it; read it back with DocumentReference.fromJson.
toString() → String
A string representation of this object.
inherited
update(Map<Object, Object?> data) → Future<void>
Updates fields in the document referred to by this DocumentReference. If no document exists yet, the update will fail.
withConverter<U extends Object?>({required FromFirestore<U> fromFirestore, required ToFirestore<U> toFirestore}) → DocumentReference<U>
Returns a reference to the same document that converts data with fromFirestore and toFirestore (type U) when reading and writing. Use withoutConverter to go back to raw maps.
withoutConverter() → DocumentReference<Map<String, Object?>>
Returns a reference to the same document that reads and writes raw Map<String, Object?> values (the equivalent of withConverter(null) in the JS SDK).

Operators

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

Constants

jsonType → const String
The value of the type field written by toJson.