DocumentSnapshot

class DocumentSnapshot

Known direct subclasses
QueryDocumentSnapshot

A QueryDocumentSnapshot contains data read from a document in your Cloud Firestore database as part of a query.


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

Controls the return value for server timestamps that have not yet been set to their final value.

Public functions

Boolean
contains(field: String)

Returns whether or not the field exists in the document.

Boolean
contains(fieldPath: FieldPath)

Returns whether or not the field exists in the document.

Boolean
equals(obj: Any?)
Boolean
Any?
get(field: String)

Returns the value at the field or null if the field doesn't exist.

Any?
get(fieldPath: FieldPath)

Returns the value at the field or null if the field or document doesn't exist.

Any?
get(
    field: String,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

Returns the value at the field or null if the field doesn't exist.

T?
<T> get(field: String, valueType: Class<T!>)

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Any?
get(
    fieldPath: FieldPath,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

Returns the value at the field or null if the field or document doesn't exist.

T?
<T> get(fieldPath: FieldPath, valueType: Class<T!>)

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

T?
<T> get(
    field: String,
    valueType: Class<T!>,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

T?
<T> get(
    fieldPath: FieldPath,
    valueType: Class<T!>,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Blob?
getBlob(field: String)

Returns the value of the field as a Blob.

Boolean?

Returns the value of the field as a boolean.

(Mutable)Map<String!, Any!>?

Returns the fields of the document as a Map or null if the document doesn't exist.

(Mutable)Map<String!, Any!>?
getData(
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

Returns the fields of the document as a Map or null if the document doesn't exist.

Date?
getDate(field: String)

Returns the value of the field as a Date.

Date?
getDate(
    field: String,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

Returns the value of the field as a Date.

DocumentReference?

Returns the value of the field as a DocumentReference.

Double?
getDouble(field: String)

Returns the value of the field as a double.

GeoPoint?

Returns the value of the field as a GeoPoint.

String
Long?
getLong(field: String)

Returns the value of the field as a long.

DocumentReference

Gets the reference to the document.

String?
getString(field: String)

Returns the value of the field as a String.

Timestamp?

Returns the value of the field as a com.google.firebase.Timestamp.

Timestamp?
getTimestamp(
    field: String,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

Returns the value of the field as a com.google.firebase.Timestamp.

Int
T?
<T> toObject(valueType: Class<T!>)

Returns the contents of the document converted to a POJO or null if the document doesn't exist.

T?
<T> toObject(
    valueType: Class<T!>,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

Returns the contents of the document converted to a POJO or null if the document doesn't exist.

String!

Public properties

SnapshotMetadata!

Extension functions

inline T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

inline T?
<T : Any?> DocumentSnapshot.getField(
    field: String,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

inline T?
<T : Any?> DocumentSnapshot.getField(fieldPath: FieldPath)

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

inline T?
<T : Any?> DocumentSnapshot.getField(
    fieldPath: FieldPath,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

inline T?

This function is deprecated. Migrate to use the KTX API from the main module: https://firebase.google.com/docs/android/kotlin-migration.

inline T?
<T : Any?> DocumentSnapshot.getField(
    field: String,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

This function is deprecated. Migrate to use the KTX API from the main module: https://firebase.google.com/docs/android/kotlin-migration.

inline T?
<T : Any?> DocumentSnapshot.getField(fieldPath: FieldPath)

This function is deprecated. Migrate to use the KTX API from the main module: https://firebase.google.com/docs/android/kotlin-migration.

inline T?
<T : Any?> DocumentSnapshot.getField(
    fieldPath: FieldPath,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

This function is deprecated. Migrate to use the KTX API from the main module: https://firebase.google.com/docs/android/kotlin-migration.

inline T?

Returns the contents of the document converted to a POJO or null if the document doesn't exist.

inline T?
<T : Any?> DocumentSnapshot.toObject(
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

Returns the contents of the document converted to a POJO or null if the document doesn't exist.

inline T?

This function is deprecated. Migrate to use the KTX API from the main module: https://firebase.google.com/docs/android/kotlin-migration.

inline T?
<T : Any?> DocumentSnapshot.toObject(
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
)

This function is deprecated. Migrate to use the KTX API from the main module: https://firebase.google.com/docs/android/kotlin-migration.

Public functions

contains

fun contains(field: String): Boolean

Returns whether or not the field exists in the document. Returns false if the document does not exist.

Parameters
field: String

the path to the field.

Returns
Boolean

true iff the field exists.

contains

fun contains(fieldPath: FieldPath): Boolean

Returns whether or not the field exists in the document. Returns false if the document does not exist.

Parameters
fieldPath: FieldPath

the path to the field.

Returns
Boolean

true iff the field exists.

equals

fun equals(obj: Any?): Boolean

exists

fun exists(): Boolean
Returns
Boolean

true if the document existed in this snapshot.

get

fun get(field: String): Any?

Returns the value at the field or null if the field doesn't exist.

Parameters
field: String

The path to the field

Returns
Any?

The value at the given field or null.

get

fun get(fieldPath: FieldPath): Any?

Returns the value at the field or null if the field or document doesn't exist.

Parameters
fieldPath: FieldPath

The path to the field

Returns
Any?

The value at the given field or null.

get

fun get(
    field: String,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): Any?

Returns the value at the field or null if the field doesn't exist.

Parameters
field: String

The path to the field

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet been set to their final value.

Returns
Any?

The value at the given field or null.

get

fun <T> get(field: String, valueType: Class<T!>): T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Parameters
field: String

The path to the field

valueType: Class<T!>

The Java class to convert the field value to.

Returns
T?

The value at the given field or null.

get

fun get(
    fieldPath: FieldPath,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): Any?

Returns the value at the field or null if the field or document doesn't exist.

Parameters
fieldPath: FieldPath

The path to the field

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet been set to their final value.

Returns
Any?

The value at the given field or null.

get

fun <T> get(fieldPath: FieldPath, valueType: Class<T!>): T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Parameters
fieldPath: FieldPath

The path to the field

valueType: Class<T!>

The Java class to convert the field value to.

Returns
T?

The value at the given field or null.

get

fun <T> get(
    field: String,
    valueType: Class<T!>,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Parameters
field: String

The path to the field

valueType: Class<T!>

The Java class to convert the field value to.

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet been set to their final value.

Returns
T?

The value at the given field or null.

get

fun <T> get(
    fieldPath: FieldPath,
    valueType: Class<T!>,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Parameters
fieldPath: FieldPath

The path to the field

valueType: Class<T!>

The Java class to convert the field value to.

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet been set to their final value.

Returns
T?

The value at the given field or null.

getBlob

fun getBlob(field: String): Blob?

Returns the value of the field as a Blob.

Parameters
field: String

The path to the field.

Returns
Blob?

The value of the field

Throws
java.lang.RuntimeException: java.lang.RuntimeException

if the value is not a Blob.

getBoolean

fun getBoolean(field: String): Boolean?

Returns the value of the field as a boolean. If the value is not a boolean this will throw a runtime exception.

Parameters
field: String

The path to the field.

Returns
Boolean?

The value of the field

getData

fun getData(): (Mutable)Map<String!, Any!>?

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.

Returns
(Mutable)Map<String!, Any!>?

The fields of the document as a Map or null if the document doesn't exist.

getData

fun getData(
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): (Mutable)Map<String!, Any!>?

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
serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet been set to their final value.

Returns
(Mutable)Map<String!, Any!>?

The fields of the document as a Map or null if the document doesn't exist.

getDate

fun getDate(field: String): Date?

Returns the value of the field as a Date.

Parameters
field: String

The path to the field.

Returns
Date?

The value of the field

Throws
java.lang.RuntimeException: java.lang.RuntimeException

if the value is not a Date.

getDate

fun getDate(
    field: String,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): Date?

Returns the value of the field as a Date.

Parameters
field: String

The path to the field.

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet been set to their final value.

Returns
Date?

The value of the field

Throws
java.lang.RuntimeException: java.lang.RuntimeException

if the value is not a Date.

getDocumentReference

fun getDocumentReference(field: String): DocumentReference?

Returns the value of the field as a DocumentReference.

Parameters
field: String

The path to the field.

Returns
DocumentReference?

The value of the field

Throws
java.lang.RuntimeException: java.lang.RuntimeException

if the value is not a DocumentReference.

getDouble

fun getDouble(field: String): Double?

Returns the value of the field as a double.

Parameters
field: String

The path to the field.

Returns
Double?

The value of the field

Throws
java.lang.RuntimeException: java.lang.RuntimeException

if the value is not a number.

getGeoPoint

fun getGeoPoint(field: String): GeoPoint?

Returns the value of the field as a GeoPoint.

Parameters
field: String

The path to the field.

Returns
GeoPoint?

The value of the field

Throws
java.lang.RuntimeException: java.lang.RuntimeException

if the value is not a GeoPoint.

getId

fun getId(): String
Returns
String

The id of the document.

getLong

fun getLong(field: String): Long?

Returns the value of the field as a long.

Parameters
field: String

The path to the field.

Returns
Long?

The value of the field

Throws
java.lang.RuntimeException: java.lang.RuntimeException

if the value is not a number.

getReference

fun getReference(): DocumentReference

Gets the reference to the document.

Returns
DocumentReference

The reference to the document.

getString

fun getString(field: String): String?

Returns the value of the field as a String.

Parameters
field: String

The path to the field.

Returns
String?

The value of the field

Throws
java.lang.RuntimeException: java.lang.RuntimeException

if the value is not a String.

getTimestamp

fun getTimestamp(field: String): Timestamp?

Returns the value of the field as a com.google.firebase.Timestamp.

Parameters
field: String

The path to the field.

Returns
Timestamp?

The value of the field

Throws
java.lang.RuntimeException: java.lang.RuntimeException

if this is not a timestamp field.

getTimestamp

fun getTimestamp(
    field: String,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): Timestamp?

Returns the value of the field as a com.google.firebase.Timestamp.

Parameters
field: String

The path to the field.

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet been set to their final value.

Returns
Timestamp?

The value of the field

Throws
java.lang.RuntimeException: java.lang.RuntimeException

if the value is not a timestamp field.

hashCode

fun hashCode(): Int

toObject

fun <T> toObject(valueType: Class<T!>): T?

Returns the contents of the document converted to a POJO or null if the document doesn't exist.

Parameters
valueType: Class<T!>

The Java class to create

Returns
T?

The contents of the document in an object of type T or null if the document doesn't exist.

toObject

fun <T> toObject(
    valueType: Class<T!>,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): T?

Returns the contents of the document converted to a POJO or null if the document doesn't exist.

Parameters
valueType: Class<T!>

The Java class to create

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet been set to their final value.

Returns
T?

The contents of the document in an object of type T or null if the document doesn't exist.

toString

fun toString(): String!

Public properties

metadata

val metadataSnapshotMetadata!

Extension functions

getField

inline fun <T : Any?> DocumentSnapshot.getField(field: String): T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Parameters
<T : Any?>

The type to convert the field value to.

field: String

The path to the field.

Returns
T?

The value at the given field or null.

getField

inline fun <T : Any?> DocumentSnapshot.getField(
    field: String,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Parameters
<T : Any?>

The type to convert the field value to.

field: String

The path to the field.

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet

        been set to their final value.
@return

The value at the given field or null.

getField

inline fun <T : Any?> DocumentSnapshot.getField(fieldPath: FieldPath): T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Parameters
<T : Any?>

The type to convert the field value to.

fieldPath: FieldPath

The path to the field.

Returns
T?

The value at the given field or null.

getField

inline fun <T : Any?> DocumentSnapshot.getField(
    fieldPath: FieldPath,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Parameters
<T : Any?>

The type to convert the field value to.

fieldPath: FieldPath

The path to the field.

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet

        been set to their final value.
@return

The value at the given field or null.

getField

inline fun <T : Any?> DocumentSnapshot.getField(field: String): T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Deprecation Notice: The Kotlin extensions (KTX) APIs have been added to their respective main modules, and the Kotlin extension (KTX) APIs in com.google.firebase.firebase-database-ktx are now deprecated. As early as April 2024, we'll no longer release KTX modules. For details, see the FAQ about this initiative.

Parameters
<T : Any?>

The type to convert the field value to.

field: String

The path to the field.

Returns
T?

The value at the given field or null.

getField

inline fun <T : Any?> DocumentSnapshot.getField(
    field: String,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Deprecation Notice: The Kotlin extensions (KTX) APIs have been added to their respective main modules, and the Kotlin extension (KTX) APIs in com.google.firebase.firebase-database-ktx are now deprecated. As early as April 2024, we'll no longer release KTX modules. For details, see the FAQ about this initiative.

Parameters
<T : Any?>

The type to convert the field value to.

field: String

The path to the field.

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet

        been set to their final value.
@return

The value at the given field or null.

getField

inline fun <T : Any?> DocumentSnapshot.getField(fieldPath: FieldPath): T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Deprecation Notice: The Kotlin extensions (KTX) APIs have been added to their respective main modules, and the Kotlin extension (KTX) APIs in com.google.firebase.firebase-database-ktx are now deprecated. As early as April 2024, we'll no longer release KTX modules. For details, see the FAQ about this initiative.

Parameters
<T : Any?>

The type to convert the field value to.

fieldPath: FieldPath

The path to the field.

Returns
T?

The value at the given field or null.

getField

inline fun <T : Any?> DocumentSnapshot.getField(
    fieldPath: FieldPath,
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): T?

Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.

Deprecation Notice: The Kotlin extensions (KTX) APIs have been added to their respective main modules, and the Kotlin extension (KTX) APIs in com.google.firebase.firebase-database-ktx are now deprecated. As early as April 2024, we'll no longer release KTX modules. For details, see the FAQ about this initiative.

Parameters
<T : Any?>

The type to convert the field value to.

fieldPath: FieldPath

The path to the field.

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet

        been set to their final value.
@return

The value at the given field or null.

toObject

inline fun <T : Any?> DocumentSnapshot.toObject(): T?

Returns the contents of the document converted to a POJO or null if the document doesn't exist.

Parameters
<T : Any?>

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.

toObject

inline fun <T : Any?> DocumentSnapshot.toObject(
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): T?

Returns the contents of the document converted to a POJO or null if the document doesn't exist.

Parameters
<T : Any?>

The type of the object to create.

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet

        been set to their final value.
@return

The contents of the document in an object of type T or null if the document doesn't

    exist.

toObject

inline fun <T : Any?> DocumentSnapshot.toObject(): T?

Returns the contents of the document converted to a POJO or null if the document doesn't exist.

Deprecation Notice: The Kotlin extensions (KTX) APIs have been added to their respective main modules, and the Kotlin extension (KTX) APIs in com.google.firebase.firebase-database-ktx are now deprecated. As early as April 2024, we'll no longer release KTX modules. For details, see the FAQ about this initiative.

Parameters
<T : Any?>

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.

toObject

inline fun <T : Any?> DocumentSnapshot.toObject(
    serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior
): T?

Returns the contents of the document converted to a POJO or null if the document doesn't exist.

Deprecation Notice: The Kotlin extensions (KTX) APIs have been added to their respective main modules, and the Kotlin extension (KTX) APIs in com.google.firebase.firebase-database-ktx are now deprecated. As early as April 2024, we'll no longer release KTX modules. For details, see the FAQ about this initiative.

Parameters
<T : Any?>

The type of the object to create.

serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior

Configures the behavior for server timestamps that have not yet

        been set to their final value.
@return

The contents of the document in an object of type T or null if the document doesn't

    exist.