DocumentSnapshot
public class DocumentSnapshot
QueryDocumentSnapshot |
A |
A DocumentSnapshot contains data read from a document in your Cloud Firestore database. The data can be extracted with the getData or get methods.
If the DocumentSnapshot points to a non-existing document, getData and its corresponding methods will return null. You can always explicitly check for a document's existence by calling exists.
Subclassing Note: Cloud 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.
Summary
Nested types |
|---|
public enum DocumentSnapshot.ServerTimestampBehaviorControls the return value for server timestamps that have not yet been set to their final value. |
Public fields |
|
|---|---|
final SnapshotMetadata |
Public methods |
|
|---|---|
boolean |
Returns whether or not the field exists in the document. |
boolean |
Returns whether or not the field exists in the document. |
boolean |
|
boolean |
exists() |
@Nullable Object |
Returns the value at the field or |
@Nullable Object |
Returns the value at the field or |
@Nullable Object |
get(Returns the value at the field or |
@Nullable T |
Returns the value at the field, converted to a POJO, or |
@Nullable Object |
get(Returns the value at the field or |
@Nullable T |
Returns the value at the field, converted to a POJO, or |
@Nullable T |
<T> get(Returns the value at the field, converted to a POJO, or |
@Nullable T |
<T> get(Returns the value at the field, converted to a POJO, or |
@Nullable Blob |
Returns the value of the field as a Blob. |
@Nullable Boolean |
getBoolean(@NonNull String field)Returns the value of the field as a boolean. |
@Nullable Map<String, Object> |
getData()Returns the fields of the document as a Map or |
@Nullable Map<String, Object> |
getData(Returns the fields of the document as a Map or |
@Nullable Date |
Returns the value of the field as a Date. |
@Nullable Date |
getDate(Returns the value of the field as a Date. |
@Nullable DocumentReference |
getDocumentReference(@NonNull String field)Returns the value of the field as a DocumentReference. |
@Nullable Double |
Returns the value of the field as a double. |
@Nullable GeoPoint |
getGeoPoint(@NonNull String field)Returns the value of the field as a GeoPoint. |
@NonNull String |
getId() |
@Nullable Long |
Returns the value of the field as a long. |
@NonNull SnapshotMetadata |
|
@NonNull DocumentReference |
Gets the reference to the document. |
@Nullable String |
Returns the value of the field as a String. |
@Nullable Timestamp |
getTimestamp(@NonNull String field)Returns the value of the field as a |
@Nullable Timestamp |
getTimestamp(Returns the value of the field as a |
@Nullable VectorValue |
getVectorValue(@NonNull String field)Returns the value of the field as a |
int |
hashCode() |
@Nullable T |
Returns the contents of the document converted to a POJO or |
@Nullable T |
<T> toObject(Returns the contents of the document converted to a POJO or |
String |
toString() |
Extension functions |
|
|---|---|
final T |
<T extends Object> FirestoreKt.getField(Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist. |
final T |
<T extends Object> FirestoreKt.getField(Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist. |
final T |
<T extends Object> FirestoreKt.getField(Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist. |
final T |
<T extends Object> FirestoreKt.getField(Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist. |
final T |
<T extends Object> FirestoreKt.toObject(@NonNull DocumentSnapshot receiver)Returns the contents of the document converted to a POJO or null if the document doesn't exist. |
final T |
<T extends Object> FirestoreKt.toObject(Returns the contents of the document converted to a POJO or null if the document doesn't exist. |
Public fields
Public methods
contains
public boolean contains(@NonNull String field)
Returns whether or not the field exists in the document. Returns false if the document does not exist.
| Returns | |
|---|---|
boolean |
true iff the field exists. |
contains
public boolean contains(@NonNull FieldPath fieldPath)
Returns whether or not the field exists in the document. Returns false if the document does not exist.
| Returns | |
|---|---|
boolean |
true iff the field exists. |
get
public @Nullable Object get(@NonNull String field)
Returns the value at the field or null if the field doesn't exist.
get
public @Nullable Object get(@NonNull FieldPath fieldPath)
Returns the value at the field or null if the field or document doesn't exist.
get
public @Nullable Object get(
@NonNull String field,
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior
)
Returns the value at the field or null if the field doesn't exist.
| Parameters | |
|---|---|
@NonNull String field |
The path to the field |
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior |
Configures the behavior for server timestamps that have not yet been set to their final value. |
get
public @Nullable T <T> get(@NonNull String field, @NonNull Class<T> valueType)
Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.
| Parameters | |
|---|---|
@NonNull String field |
The path to the field |
@NonNull Class<T> valueType |
The Java class to convert the field value to. |
| Returns | |
|---|---|
@Nullable T |
The value at the given field or |
get
public @Nullable Object get(
@NonNull FieldPath fieldPath,
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior
)
Returns the value at the field or null if the field or document doesn't exist.
| Parameters | |
|---|---|
@NonNull FieldPath fieldPath |
The path to the field |
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior |
Configures the behavior for server timestamps that have not yet been set to their final value. |
get
public @Nullable T <T> get(@NonNull FieldPath fieldPath, @NonNull Class<T> valueType)
Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.
| Parameters | |
|---|---|
@NonNull FieldPath fieldPath |
The path to the field |
@NonNull Class<T> valueType |
The Java class to convert the field value to. |
| Returns | |
|---|---|
@Nullable T |
The value at the given field or |
get
public @Nullable T <T> get(
@NonNull String field,
@NonNull Class<T> valueType,
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior
)
Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.
| Parameters | |
|---|---|
@NonNull String field |
The path to the field |
@NonNull Class<T> valueType |
The Java class to convert the field value to. |
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior |
Configures the behavior for server timestamps that have not yet been set to their final value. |
| Returns | |
|---|---|
@Nullable T |
The value at the given field or |
get
public @Nullable T <T> get(
@NonNull FieldPath fieldPath,
@NonNull Class<T> valueType,
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior
)
Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.
| Parameters | |
|---|---|
@NonNull FieldPath fieldPath |
The path to the field |
@NonNull Class<T> valueType |
The Java class to convert the field value to. |
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior |
Configures the behavior for server timestamps that have not yet been set to their final value. |
| Returns | |
|---|---|
@Nullable T |
The value at the given field or |
getBlob
public @Nullable Blob getBlob(@NonNull String field)
Returns the value of the field as a Blob.
| Throws | |
|---|---|
java.lang.RuntimeException java.lang.RuntimeException |
if the value is not a Blob. |
getBoolean
public @Nullable Boolean getBoolean(@NonNull String field)
Returns the value of the field as a boolean. If the value is not a boolean this will throw a runtime exception.
getData
public @Nullable Map<String, Object> getData()
Returns the fields of the document as a Map or null if the document doesn't exist. Field values will be converted to their native Java representation.
getData
public @Nullable Map<String, Object> getData(
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior
)
Returns the fields of the document as a Map or null if the document doesn't exist. Field values will be converted to their native Java representation.
| Parameters | |
|---|---|
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior |
Configures the behavior for server timestamps that have not yet been set to their final value. |
getDate
public @Nullable Date getDate(@NonNull String field)
Returns the value of the field as a Date.
| Throws | |
|---|---|
java.lang.RuntimeException java.lang.RuntimeException |
if the value is not a Date. |
getDate
public @Nullable Date getDate(
@NonNull String field,
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior
)
Returns the value of the field as a Date.
| Parameters | |
|---|---|
@NonNull String field |
The path to the field. |
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior |
Configures the behavior for server timestamps that have not yet been set to their final value. |
| Throws | |
|---|---|
java.lang.RuntimeException java.lang.RuntimeException |
if the value is not a Date. |
getDocumentReference
public @Nullable DocumentReference getDocumentReference(@NonNull String field)
Returns the value of the field as a DocumentReference.
| Returns | |
|---|---|
@Nullable DocumentReference |
The value of the field |
| Throws | |
|---|---|
java.lang.RuntimeException java.lang.RuntimeException |
if the value is not a DocumentReference. |
getDouble
public @Nullable Double getDouble(@NonNull String field)
Returns the value of the field as a double.
| Throws | |
|---|---|
java.lang.RuntimeException java.lang.RuntimeException |
if the value is not a number. |
getGeoPoint
public @Nullable GeoPoint getGeoPoint(@NonNull String field)
Returns the value of the field as a GeoPoint.
| Throws | |
|---|---|
java.lang.RuntimeException java.lang.RuntimeException |
if the value is not a GeoPoint. |
getLong
public @Nullable Long getLong(@NonNull String field)
Returns the value of the field as a long.
| Throws | |
|---|---|
java.lang.RuntimeException java.lang.RuntimeException |
if the value is not a number. |
getMetadata
public @NonNull SnapshotMetadata getMetadata()
| Returns | |
|---|---|
@NonNull SnapshotMetadata |
The metadata for this document snapshot. |
getReference
public @NonNull DocumentReference getReference()
Gets the reference to the document.
| Returns | |
|---|---|
@NonNull DocumentReference |
The reference to the document. |
getString
public @Nullable String getString(@NonNull String field)
Returns the value of the field as a String.
| Throws | |
|---|---|
java.lang.RuntimeException java.lang.RuntimeException |
if the value is not a String. |
getTimestamp
public @Nullable Timestamp getTimestamp(@NonNull String field)
Returns the value of the field as a com.google.firebase.Timestamp.
| Throws | |
|---|---|
java.lang.RuntimeException java.lang.RuntimeException |
if this is not a timestamp field. |
getTimestamp
public @Nullable Timestamp getTimestamp(
@NonNull String field,
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior
)
Returns the value of the field as a com.google.firebase.Timestamp.
| Parameters | |
|---|---|
@NonNull String field |
The path to the field. |
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior |
Configures the behavior for server timestamps that have not yet been set to their final value. |
| Throws | |
|---|---|
java.lang.RuntimeException java.lang.RuntimeException |
if the value is not a timestamp field. |
getVectorValue
public @Nullable VectorValue getVectorValue(@NonNull String field)
Returns the value of the field as a VectorValue or null if the field does not exist in the document.
| Returns | |
|---|---|
@Nullable VectorValue |
The value of the field. |
| Throws | |
|---|---|
java.lang.RuntimeException java.lang.RuntimeException |
if the value is not a |
toObject
public @Nullable T <T> toObject(@NonNull Class<T> valueType)
Returns the contents of the document converted to a POJO or null if the document doesn't exist.
| Returns | |
|---|---|
@Nullable T |
The contents of the document in an object of type T or |
toObject
public @Nullable T <T> toObject(
@NonNull Class<T> valueType,
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior
)
Returns the contents of the document converted to a POJO or null if the document doesn't exist.
| Parameters | |
|---|---|
@NonNull Class<T> valueType |
The Java class to create |
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior |
Configures the behavior for server timestamps that have not yet been set to their final value. |
| Returns | |
|---|---|
@Nullable T |
The contents of the document in an object of type T or |
Extension functions
FirestoreKt.getField
public final T <T extends Object> FirestoreKt.getField(
@NonNull DocumentSnapshot receiver,
@NonNull String field
)
Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.
| Parameters | |
|---|---|
<T extends Object> |
The type to convert the field value to. |
@NonNull String field |
The path to the field. |
| Returns | |
|---|---|
T |
The value at the given field or null. |
FirestoreKt.getField
public final T <T extends Object> FirestoreKt.getField(
@NonNull DocumentSnapshot receiver,
@NonNull String field,
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior
)
Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.
| Parameters | |
|---|---|
<T extends Object> |
The type to convert the field value to. |
@NonNull String field |
The path to the field. |
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior |
Configures the behavior for server timestamps that have not yet been set to their final value. The value at the given field or null. |
FirestoreKt.getField
public final T <T extends Object> FirestoreKt.getField(
@NonNull DocumentSnapshot receiver,
@NonNull FieldPath fieldPath
)
Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.
| Parameters | |
|---|---|
<T extends Object> |
The type to convert the field value to. |
@NonNull FieldPath fieldPath |
The path to the field. |
| Returns | |
|---|---|
T |
The value at the given field or null. |
FirestoreKt.getField
public final T <T extends Object> FirestoreKt.getField(
@NonNull DocumentSnapshot receiver,
@NonNull FieldPath fieldPath,
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior
)
Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.
| Parameters | |
|---|---|
<T extends Object> |
The type to convert the field value to. |
@NonNull FieldPath fieldPath |
The path to the field. |
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior |
Configures the behavior for server timestamps that have not yet been set to their final value. The value at the given field or null. |
FirestoreKt.toObject
public final T <T extends Object> FirestoreKt.toObject(@NonNull DocumentSnapshot receiver)
Returns the contents of the document converted to a POJO or null if the document doesn't exist.
| Parameters | |
|---|---|
<T extends Object> |
The type of the object to create. |
| Returns | |
|---|---|
T |
The contents of the document in an object of type T or null if the document doesn't exist. |
FirestoreKt.toObject
public final T <T extends Object> FirestoreKt.toObject(
@NonNull DocumentSnapshot receiver,
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior
)
Returns the contents of the document converted to a POJO or null if the document doesn't exist.
| Parameters | |
|---|---|
<T extends Object> |
The type of the object to create. |
@NonNull DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior |
Configures the behavior for server timestamps that have not yet been set to their final value. The contents of the document in an object of type T or null if the document doesn't exist. |