firefuel 0.0.2+1 firefuel: ^0.0.2+1 copied to clipboard
A Firebase Cloud Firestore library to fuel your application's growth!
Overview #
The goal of this package is to make it easy to interact with Cloud Firestore API. The firefuel
community aims to always make this package simple, intuitive, and consistent. firefuel
wraps the cloud_firestore plugin, and provides conventions to help jump-start your development.
Still not convinced? See our documentation on why we thing you should choose firefuel
Scope #
firefuel
focuses on simplifying the edge of your data layer, meaning this package pairs well with all ui, state management, model generation, and injection packages.
Getting Started #
Simply add the latest version of firefuel
as a dependency in your pubspec.yaml
file. Import package:firefuel/firefuel.dart
into your entry point (often main.dart
). Then, initialize firefuel
using the Firefuel.initialize(FirebaseFirestore.instance);
before calling runApp
.
Read the full walkthrough in our docs.
Quick Start #
Choose a collection from your Firestore db and create a class to model your document. For this example let's assume you have a collection of users with a username, first name, last name, and favorite color.
Each model needs to extend Serializable
so firefuel
is able to automatically convert the model to JSON. We'll also want to add a fromJson
method that we'll use to convert it from json into an instance of the model.
Most of the time, when comparing two models of the same type, you want to know whether the two instances have identical values. However, by default, Dart will compare whether the instances reference the same object in memory. I suggest using the equatable
package with your models to compare by value rather than by reference.
Create a Model #
class User extends Serializable with EquatableMixin {
static const String fieldUsername = 'username', fieldUsername = 'firstName',fieldUsername = 'lastName',fieldUsername = 'favoriteColor',;
const User(this.name, {this.docId, this.age, this.occupation});
final String name;
final int? age;
final String? docId;
final String? occupation;
@override
List<Object?> get props => [name, age, occupation];
factory User.fromJson(Map<String, dynamic> json, String docId) {
return User(
json[fieldName],
age: json[fieldAge],
docId: docId,
occupation: json[fieldOccupation],
);
}
@override
Map<String, dynamic> toJson() {
return {fieldName: name, fieldAge: age, fieldOccupation: occupation};
}
}
Create a Collection #
class UserCollection extends FirefuelCollection<User> {
Collection() : super(collectionName);
static const collectionName = 'users';
@override
User? fromFirestore(
DocumentSnapshot<Map<String, dynamic>> snapshot,
SnapshotOptions? options,
) {
final data = snapshot.data();
return data == null
? null
: User.fromJson(snapshot.data()!, snapshot.id);
}
@override
Map<String, Object?> toFirestore(User? model, SetOptions? options) {
return model?.toJson() ?? <String, Object?>{};
}
}
Profit #
That's it! Now you can access your data through the UserCollection
with any of the following methods.
Related Links #
See the firefuel documentation to walk through the core concepts of using firefuel
.
See also: Handling Errors
Issues and feedback #
Please file all firefuel
specific issues, bugs, or feature requests in our issue tracker
Please file FlutterFire
specific issues, bugs, or feature requests in their issue tracker.
Plugin issues that are not specific to FlutterFire can be filed in the Flutter issue tracker.
Maintainers #
The maintainers for firefuel
are Jonah Walker and
Morgan Hunt
Logo Creator #
Our logo was created by Shawn Meek