data method

T? data({
  1. ServerTimestampBehavior serverTimestamps = ServerTimestampBehavior.none,
})

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 T instead, and the converter gets serverTimestamps in its SnapshotOptions.

A server timestamp (FieldValue.serverTimestamp()) that the backend has not set yet is null by default (ServerTimestampBehavior.none); pass ServerTimestampBehavior.estimate or ServerTimestampBehavior.previous as serverTimestamps to get a value for it.

final DocumentSnapshot<Map<String, Object?>> snapshot = await firestore
    .doc('cities/SF')
    .get();
final Map<String, Object?>? data = snapshot.data();
print(data?['name']);

// Shows a local estimate until the backend sets the timestamp.
final Map<String, Object?>? estimated = snapshot.data(
  serverTimestamps: ServerTimestampBehavior.estimate,
);
print(estimated?['updated']);

Implementation

T? data({
  ServerTimestampBehavior serverTimestamps = ServerTimestampBehavior.none,
}) {
  final Document? doc = document;
  if (doc == null) {
    return null;
  }
  final FirestoreDataConverter<T>? converter = _converter;
  if (converter == null) {
    return _rawData(doc, serverTimestamps) as T;
  }
  final QueryDocumentSnapshot<Map<String, Object?>> raw =
      QueryDocumentSnapshotInternal.fromDocument<Map<String, Object?>>(
        _firestore,
        doc,
        fromCache: metadata.isFromCache,
        hasPendingWrites: metadata.hasPendingWrites,
      );
  return converter.fromFirestore(
    raw,
    SnapshotOptions(serverTimestamps: serverTimestamps),
  );
}