Firefuel Basics
firefuel is a Flutter package created to make using Firebase Cloud Firestore
easier.
As developers, we want to:
- know our data will get converted into type-safe code
- have a simple, intuitive, and consistent way to interact with data.
- know what collections and subcollections you have access to
- develop fast and reactive apps.
Firefuel
was designed to meet all of these needs and many more.
Add Dependencies
Run the following commands in your terminal
after running the above commands you should end up with a pubspec.yaml
file that looks similar to the below gist.
Note: If the above commands didn’t work, just find the latest versions of the below packages and add them to your pubspec.yaml
file manually.
After installing the dependencies and following the installation instructions for FlutterFire. You’re ready to initialize your app with firefuel
! In your main.dart
file add the following:
Important!
cloud_firestore
requires the minimum iOS Deployment Target to be set to 10.0 in Xcode
.
Additionally, once you generate your ios/Podfile
by running your app on an iPhone (or by running cd ios; pod update;
from your terminal), you’ll want to uncomment the second line and update it to platform :ios, ‘10.0’
.
Ensure Type Safety
It’s recommended to use the Equatable
package, but is not required.
To convert any document into type-safe code, you’ll want a class you can use to model your data. In this example, we’ll use the class User
.
firefuel
requires any model you create to extend Serializable
. Serializable requires you to implement a toJson
method. We also suggest implementing a factory
or named constructor
to convert the Map<String, dynamic>
from Firestore into an instance of your model.
Now that we have our User
class as our model, we’ll be able to use this class to create a FirefuelCollection
.
FirefuelCollection
A FirefuelCollection
gives you a simple, intuitive, and consistent way to interact with data. Once you inherit from a FirefuelCollection
, you will get create
, read
, update
and delete
actions for your model (along with others).
Create A Collection
Let’s create a UserCollection
so we can consistently access our data in a type-safe way.
Since you’ll create a collection for each document type you want to model, this class is responsible for providing the functionality to convert your data to and from JSON.
The UserCollection
class is also responsible for providing the name of your collection to the FirefuelCollection
super class.
Usage
See the full FirefuelCollection API reference for all available methods.
Reference Subcollections
To access a subcollection, you need the document id of the document you want to access the subcollection on. In firefuel, we want to access the subcollection with a syntax similar to userCollection.read(docId).friends.readAll()
.
Follow the steps above for creating a model and collection. Then add a new method to the FriendCollection
so you can build the appropriate collection path for the subcollection.
If you want to, you can add a method to your UserCollection
where you pass in a User to build the subcollection path. However, I suggest using an extension class for this.
Now you’re able to access all CRUD applications on your subcollection!
Thanks for reading! If you enjoyed this article, please clap up to 50 times to make sure other devs can find it.
What’s Next
- Find more details at firefuel.dev
- Take a look at the firefuel API to see all available classes and methods
- Submit any feedback, feature requests, bugs, questions, and pull requests on the firefuel repository.