withConverter<R> method

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

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

This makes both read and write operations type-safe.

final personsRef = FirebaseFirestore
    .instance
    .collection('persons')
    .where('age', isGreaterThan: 0)
    .withConverter<Person>(
      fromFirestore: (snapshot, _) => Person.fromJson(snapshot.data()!),
      toFirestore: (person, _) => person.toJson(),
    );

Future<void> main() async {
  List<QuerySnapshot<Person>> persons = await personsRef.get().then((s) => s.docs);
}

Implementation

@override
_i2.Query<R> withConverter<R>({_i2.FromFirestore<R>? fromFirestore, _i2.ToFirestore<R>? toFirestore}) =>
    (super.noSuchMethod(Invocation.method(#withConverter, [], {#fromFirestore: fromFirestore, #toFirestore: toFirestore}),
        returnValue: _FakeQuery<R>()) as _i2.Query<R>);