0 purchases
database firebase helper
database_firebase_helper #
A Flutter (pure-dart) package to simpler FromMap/ToJson when working with Firebase Firestore
Inspired by the implementation on Java :)
Heavily based on the great package reflectable
Getting Started #
Setup #
Make sure in main.dart:
Add reflectable generated file import:
import 'main.reflectable.dart';
copied to clipboard
On the main() function add:
initializeReflectable();
copied to clipboard
In order to set up reflection support.
Annotate your class #
@firebaseReflector // ---> This annotation enables reflection on your class
class MyClass{
@Id // ---> This annotation marks member as Document's id
String mId;
@MapTo('name') // ---> This annotation marks member as the field in the Document
String mName;
@MapTo('age')
int mAge;
}
copied to clipboard
Build #
Use build_runner to generate the 'main.reflectable.dart' file:
$ flutter pub run build_runner build
copied to clipboard
You have to run it each time you modify your classes
Usage #
On a document snapshot #
Get your object directly from the snapshot, for example:
DocSnapshot snapshot = FirebaseFirestore.getInstance.doc(<doc_path>)
var myClass = snaphot.getValue<MyClass>(snapshot);
copied to clipboard
You can also use set and update function from your object, for example:
DocSnapshot snapshot = FirebaseFirestore.getInstance.doc("myCollection/myDoc")
// set
snaphot.set(<My Class Instance>);
// update
snaphot.update(<My Class Instance>);
copied to clipboard
You can also can convert Object into Map, for example:
Map jsonMap = DatabaseFirebaseHelper.toJson(<My Class Instance>);
copied to clipboard
On a collection snapshot #
You can use add function using from an object, for example:
var snapshot = FirebaseFirestore.getInstance.collection("myCollection");
snapshot.add(<My Class Instance>);
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.