withConverter<U extends Object?> method

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

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

The references returned by doc and add keep the converter.

final CollectionReference<String> names = firestore
    .collection('cities')
    .withConverter<String>(
      fromFirestore: (snapshot, options) => snapshot['name'] as String,
      toFirestore: (name, options) => {'name': name},
    );
await names.doc('LIS').set('Lisbon');
final DocumentSnapshot<String> lisbon = await names.doc('LIS').get();
print(lisbon.data()); // Lisbon

Implementation

@override
CollectionReference<U> withConverter<U extends Object?>({
  required FromFirestore<U> fromFirestore,
  required ToFirestore<U> toFirestore,
}) {
  return CollectionReference<U>._(
    query.path,
    firestore,
    FirestoreDataConverter<U>.from(
      fromFirestore: fromFirestore,
      toFirestore: toFirestore,
    ),
  );
}