withConverter<R> method
DocumentReference<R>
withConverter<R>({
- FromFirestore<
R> ? fromFirestore, - 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
_i2.DocumentReference<R> withConverter<R>({_i2.FromFirestore<R>? fromFirestore, _i2.ToFirestore<R>? toFirestore}) =>
(super.noSuchMethod(Invocation.method(#withConverter, [], {#fromFirestore: fromFirestore, #toFirestore: toFirestore}),
returnValue: _FakeDocumentReference<R>()) as _i2.DocumentReference<R>);