withConverter<R> method

  1. @override
DocumentReference<R> withConverter<R>({
  1. required FromFirestore<R> fromFirestore,
  2. required ToFirestore<R> toFirestore,
})
override

Transforms a DocumentReference to manipulate a custom object instead of a Map<String, dynamic>.

This makes both read and write operations type-safe.

final modelRef = FirebaseFirestore
    .instance
    .collection('models')
    .doc('123')
    .withConverter<Model>(
      fromFirestore: (snapshot, _) => Model.fromJson(snapshot.data()!),
      toFirestore: (model, _) => model.toJson(),
    );

Future<void> main() async {
  // Writes now take a Model as parameter instead of a Map
  await modelRef.set(Model());

  // Reads now return a Model instead of a Map
  final Model model = await modelRef.get().then((s) => s.data());
}

Implementation

@override
DocumentReference<R> withConverter<R>({
  required FromFirestore<R> fromFirestore,
  required ToFirestore<R> toFirestore,
}) =>
    throw UnimplementedError();