FirestoreDataConverter<T> class abstract

Converts between your own model class T and the raw field map stored in Firestore.

withConverter on a DocumentReference, CollectionReference or Query takes the two functions (FromFirestore and ToFirestore) and keeps them as a converter, which the converter property of the reference or query returns. Implement this class, or build one from two functions with FirestoreDataConverter.from, to pass a converter to fromJson:

final DocumentReference<City> sf = firestore
    .doc('cities/SF')
    .withConverter<City>(
      fromFirestore: (snapshot, options) => City.fromMap(snapshot.data()!),
      toFirestore: (city, options) => city.toMap(),
    );
await sf.set(City('San Francisco'));
final City? city = (await sf.get()).data();

The converter runs on the client only. toFirestore runs when you write with set or add (not with update, which always takes a map), and fromFirestore runs each time you read data() of a snapshot of an existing document. An error that either throws reaches the caller of that write or read.

withoutConverter() on a reference or query returns to raw maps. In package:cloud_firestore_dart/lite.dart the converter is a different class whose fromFirestore takes only the snapshot.

Constructors

FirestoreDataConverter()
Const constructor for subclasses.
const
FirestoreDataConverter.from({required FromFirestore<T> fromFirestore, required ToFirestore<T> toFirestore})
Creates a converter from two functions. The returned converters compare equal when both functions are equal (top level functions, static methods and constructor tear-offs are canonical, so this works for them).
const
factory

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

fromFirestore(DocumentSnapshot<Map<String, Object?>> snapshot, SnapshotOptions? options) → T
Called by the SDK to convert a snapshot of an existing document into T. snapshot is an untyped snapshot (at runtime a QueryDocumentSnapshot, so snapshot.data() is never null). Use snapshot.data(serverTimestamps: options?.serverTimestamps ?? ServerTimestampBehavior.none) to honour the requested server timestamp behavior.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toFirestore(T value, SetOptions? options) → Map<String, Object?>
Called by the SDK to convert value into the map that is written. options are the SetOptions of the write, or null (for example for add).
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited