withConverter<R extends Object?> method
CollectionReference<R>
withConverter<R extends Object?>({
- FromFirestore<
R> ? fromFirestore, - ToFirestore<
R> ? toFirestore,
override
Transforms a CollectionReference
to manipulate a custom object instead
of a Map<String, dynamic>
.
This makes both read and write operations type-safe.
final modelsRef = FirebaseFirestore
.instance
.collection('models')
.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 modelsRef.add(Model());
// Reads now return a Model instead of a Map
final Model model = await modelsRef.doc('123').get().then((s) => s.data());
}
Implementation
@override
_i2.CollectionReference<R> withConverter<R extends Object?>(
{_i2.FromFirestore<R>? fromFirestore, _i2.ToFirestore<R>? toFirestore}) =>
(super.noSuchMethod(Invocation.method(#withConverter, [], {#fromFirestore: fromFirestore, #toFirestore: toFirestore}),
returnValue: _FakeCollectionReference<R>()) as _i2.CollectionReference<R>);