withConverter<U extends Object?> method

DocumentReference<U> withConverter<U extends Object?>({
  1. required FromFirestore<U> fromFirestore,
  2. required ToFirestore<U> toFirestore,
})

Returns a reference to the same document that converts data with fromFirestore and toFirestore (type U) when reading and writing. Use withoutConverter to go back to raw maps.

set then takes a U, and snapshots return a U from data(). update still takes a map of fields, as in the other SDKs.

final DocumentReference<String> name = firestore
    .doc('cities/SF')
    .withConverter<String>(
      fromFirestore: (snapshot, options) => snapshot['name'] as String,
      toFirestore: (name, options) => {'name': name},
    );
await name.set('San Francisco');

Implementation

DocumentReference<U> withConverter<U extends Object?>({
  required FromFirestore<U> fromFirestore,
  required ToFirestore<U> toFirestore,
}) {
  return DocumentReference<U>._(
    key,
    firestore,
    FirestoreDataConverter<U>.from(
      fromFirestore: fromFirestore,
      toFirestore: toFirestore,
    ),
  );
}