firestore_converter 1.0.4 copy "firestore_converter: ^1.0.4" to clipboard
firestore_converter: ^1.0.4 copied to clipboard

FirestoreConverter generates Firestore withConverter implementations for the annotated class.

Build pub package

This packages provides you with a new annotation @FirestoreConverter(defaultPath: 'someDataPath') to easily generate Firestore with_converter implementations in order to reduce boilerplate code for data models.

It is best used in conjunction with other annotations like freezed or json_serializable, but it is not a requirement.

Installation #

To use firestore_converter, you will need your typical build_runner/code-generator setup.
First, install build_runner and firestore_converter by adding them to your pubspec.yaml file:

flutter pub add firestore_converter_annotation
flutter pub add dev:build_runner
flutter pub add dev:firestore_converter

Since firestore_converter relies on instance.toJson to be present in the annotated model class, you should probably also add either freezed, json_serializable or some other annotation that will provide you with a convenient implementation of toJson.

Usage #

Annotate a data class with @FirestoreConverter(defaultPath: 'someDataPathInFirestore') to generate two helper functions:

  • ${modelClassName}Collection
  • ${modelClassName}Doc

Please note that this will be functions, not members since there is currently no way to add static functions via code generation to the model class. The initial letter of the model name will be converted to lowercase, to conform with the dart function naming conventions.

Things to note #

  • JSON conversion with fromJson and toJson must be enabled / implemented.

  • Uses the given path as default argument for collection and doc.

  • Does not support default settings from build.yaml since path settings are individual per model class.

Example using firestore_converter with freezed #

Define your model class, e.g. example.dart:

part 'example.firestore_converter.dart';
part 'example.freezed.dart';
part 'example.g.dart';

@freezed
@FirestoreConverter(defaultPath: 'examples')
class Example<T> with _$Example<T> {
  factory Example(int a) = _Example;
  factory Example.fromJson(Map<String, Object?> json) => _$ExampleFromJson(json);
}

Start using it:


// Now you can easily create fully typed CollectionReferences,  DocumentReferences,
// or SnapShot objects from Firebase:
CollectionReference<Example> col1 = exampleCollection().orderBy('date', descending: true); // defaults to annotated path 'examples'
CollectionReference<Example> col2 = exampleCollection('some_other_path').orderBy('a', descending: true);
DocumentReference<Example> doc = exampleDoc(docId: 'exampleId'); // defaults to annotated path 'examples'
DocumentReference<Example> doc = exampleDoc(path: 'some_other_path', docId: 'exampleId');

// retrieve some document data
var myDoc = exampleDoc(path: 'some_other_path', docId: 'exampleId').get();
// Access typed members directly from the snapshot
debugPrint(myDoc.data().a);

References #

Read the Firebase documentation about withConverter here:

https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/DocumentReference/withConverter.html

https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/CollectionReference/withConverter.html

2
likes
0
points
127
downloads

Publisher

unverified uploader

Weekly Downloads

FirestoreConverter generates Firestore withConverter implementations for the annotated class.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

analyzer, build, firestore_converter_annotation, meta, source_gen

More

Packages that depend on firestore_converter