DocumentSnapshot<T extends dynamic> class
A DocumentSnapshot contains data read from a document in your FirebaseFirestore database. The data can be extracted with the data or get methods.
If the DocumentSnapshot points to a non-existing document, data and its corresponding methods will return null. You can always explicitly check for a document's existence by calling exists.
A snapshot is immutable: it holds the document as it was when it was read. metadata tells whether it came from
the local cache and whether it includes local writes that the backend has not acknowledged yet. Values are
returned as Dart types: numbers as int or double, timestamps as Timestamp, references as
DocumentReference, bytes as Blob, maps as Map<String, Object?> and arrays as List<Object?>.
Unlike the JS SDK, data is a getter, not a method. Snapshots come from DocumentReference.get,
DocumentReference.snapshots, Transaction.get and, as QueryDocumentSnapshot, from query results.
final DocumentSnapshot<Map<String, Object?>> snapshot = await firestore
.doc('cities/SF')
.get();
if (snapshot.exists) {
final Map<String, Object?> data = snapshot.data()!;
print(data['name']);
print(snapshot['population']); // the same as snapshot.get('population')
print((snapshot.get('founded') as Timestamp?)?.toDate());
}
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
-
DocumentSnapshot.fromJson(FirebaseFirestore firestore, Map<
String, Object?> json, {FirestoreDataConverter<T> ? converter}) -
Builds a DocumentSnapshot from the JSON object written by toJson, for the
firestoreinstance.factory
Properties
- exists → bool
-
Returns true if the document existed in this snapshot.
no setter
- 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
- metadata → SnapshotMetadata
-
The metadata for this document snapshot: whether it came from the cache
(SnapshotMetadata.isFromCache) and whether it has local writes that the backend has not acknowledged yet
(SnapshotMetadata.hasPendingWrites).
final
-
reference
→ DocumentReference<
T> -
The reference to the document, with the converter of this snapshot.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
contains(
Object field) → bool - Whether the field exists in the document, also when its value is null. False if the document does not exist.
-
data(
{ServerTimestampBehavior serverTimestamps = ServerTimestampBehavior.none}) → T? -
The fields of the document, or null if the document doesn't exist. Field values are converted to their
Dart representation. With a converter (see DocumentReference.withConverter) it returns the converted
Tinstead, and the converter getsserverTimestampsin its SnapshotOptions. -
get(
Object field, {ServerTimestampBehavior serverTimestamps = ServerTimestampBehavior.none}) → Object? -
The value of
field, or null if the field or the document doesn't exist. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toJson(
) → Map< String, Object?> -
Returns a JSON-serializable representation of this snapshot, which DocumentSnapshot.fromJson reads back and
FirebaseFirestore.documentSnapshotsFromJsonlistens from. -
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override
-
operator [](
Object field) → Object? -
The value of
field, ornullif the field or the document does not exist. Same as get.