DocumentSnapshot

public class DocumentSnapshot extends Object

A DocumentSnapshot contains data read from a document in your Firestore database. The data can be extracted with the getData or get methods.

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.

Public Method Summary

boolean
contains(String field)
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
exists()
Object
get(FieldPath fieldPath)
Returns the value at the field or null if the field doesn't exist.
Object
get(String field)
Returns the value at the field or null if the field doesn't exist.
Blob
getBlob(String field)
Returns the value of the field as a Blob.
Boolean
getBoolean(String field)
Returns the value of the field as a boolean.
Map<StringObject>
getData()
Returns the fields of the document as a Map.
Date
getDate(String field)
Returns the value of the field as a Date.
DocumentReference
getDocumentReference(String field)
Returns the value of the field as a DocumentReference.
Double
getDouble(String field)
Returns the value of the field as a double.
GeoPoint
getGeoPoint(String field)
Returns the value of the field as a GeoPoint.
String
getId()
Long
getLong(String field)
Returns the value of the field as a long.
SnapshotMetadata
DocumentReference
getReference()
Gets the reference to the document.
String
getString(String field)
Returns the value of the field as a String.
<T> T
toObject(Class<T> valueType)
Returns the contents of the document converted to a POJO.

Inherited Method Summary

Public Methods

public boolean contains (String field)

Returns whether or not the field exists in the document.

Parameters
field the path to the field.
Returns
  • true iff the field exists.
Throws
IllegalStateException if the document doesn't exist.

public boolean contains (FieldPath fieldPath)

Returns whether or not the field exists in the document.

Parameters
fieldPath the path to the field.
Returns
  • true iff the field exists.
Throws
IllegalStateException if the document doesn't exist.

public boolean exists ()

Returns
  • true if the document existed in this snapshot.

public Object get (FieldPath fieldPath)

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

Parameters
fieldPath The path to the field
Returns
  • The value at the given field or null.
Throws
IllegalStateException if the document doesn't exist.

public Object get (String field)

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

Parameters
field The path to the field
Returns
  • The value at the given field or null.
Throws
IllegalStateException if the document doesn't exist.

public Blob getBlob (String field)

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

Parameters
field The path to the field.
Returns
  • The value of the field
Throws
IllegalStateException if the document doesn't exist.

public Boolean getBoolean (String field)

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

Parameters
field The path to the field.
Returns
  • The value of the field
Throws
IllegalStateException if the document doesn't exist.

public Map<StringObject> getData ()

Returns the fields of the document as a Map. Field values will be converted to their native Java representation.

Returns
  • The fields of the document as a Map.
Throws
IllegalStateException if the document doesn't exist.

public Date getDate (String field)

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

Parameters
field The path to the field.
Returns
  • The value of the field
Throws
IllegalStateException if the document doesn't exist.

public DocumentReference getDocumentReference (String field)

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

Parameters
field The path to the field.
Returns
  • The value of the field
Throws
IllegalStateException if the document doesn't exist.

public Double getDouble (String field)

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

Parameters
field The path to the field.
Returns
  • The value of the field
Throws
IllegalStateException if the document doesn't exist.

public GeoPoint getGeoPoint (String field)

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

Parameters
field The path to the field.
Returns
  • The value of the field
Throws
IllegalStateException if the document doesn't exist.

public String getId ()

Returns
  • The id of the document.

public Long getLong (String field)

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

Parameters
field The path to the field.
Returns
  • The value of the field
Throws
IllegalStateException if the document doesn't exist.

public SnapshotMetadata getMetadata ()

Returns
  • The metadata for this document snapshot.

public DocumentReference getReference ()

Gets the reference to the document.

Returns
  • The reference to the document.

public String getString (String field)

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

Parameters
field The path to the field.
Returns
  • The value of the field
Throws
IllegalStateException if the document doesn't exist.

public T toObject (Class<T> valueType)

Returns the contents of the document converted to a POJO.

Parameters
valueType The Java class to create
Returns
  • The contents of the document in an object of type T
Throws
IllegalStateException if the document doesn't exist.

Send feedback about...

Need help? Visit our support page.