data method

T? data()

Retrieves all fields in the document as an object. Returns 'undefined' if the document doesn't exist.

Returns an object containing all fields in the document or 'null' if the document doesn't exist.

final documentRef = firestore.doc('col/doc');

documentRef.get().then((documentSnapshot) {
  final data = documentSnapshot.data();
  print('Retrieved data: ${JSON.stringify(data)}');
});

Implementation

T? data() {
  final fieldsProto = this._fieldsProto;
  final fields = fieldsProto?.fields;
  if (fields == null || fieldsProto == null) return null;

  final converter = ref._converter;
  // We only want to use the converter and create a new QueryDocumentSnapshot
  // if a converter has been provided.
  if (!identical(converter, _jsonConverter)) {
    final untypedReference = DocumentReference._(
      firestore: ref.firestore,
      path: ref._path,
      converter: _jsonConverter,
    );

    return converter.fromFirestore(
      QueryDocumentSnapshot._(
        ref: untypedReference,
        fieldsProto: fieldsProto,
        readTime: readTime,
        createTime: createTime,
        updateTime: updateTime,
      ),
    );
  } else {
    final object = <String, Object?>{
      for (final prop in fields.entries)
        prop.key: ref.firestore._serializer.decodeValue(prop.value),
    };

    return object as T;
  }
}