Setting up the Firebase Firestore
The store module integrates Firestore in your project. This enables a flexible, scalable NoSQL cloud database that also works offline!
Setup
-
Create the database from the Firebase console
-
Open a terminal an run the following command from the root of the project:
- Select
Firestore
from the list of available Firebase services - Select your project.
- (Optional) Use the existing
firestore.rules
andfirestore.indexes.json
files.
- Select
-
(Optional) Deploy the pre-configured rules and indexes:
You should now be able to see the database in the Firebase console and use the store_view.dart
.
The basics
To learn how to use Firestore, we recommend checking the official documentation, we will only cover ShipFlutter specifics here.
Directorylib/modules/store
- store.dart
- store_service.dart
- store_controller.dart
- store_item.dart
- store_view.dart
- store_route.dart
- json_converters.dart
The main class is FirebaseStore
located in store.dart
. Use this class to wrap a document or collection and retrieve
data via its methods, and converting them to your data model.
The root
is the path to the collection (e.g users
or store/1234/purchases
).
The fromFirestore
and toFirestore
methods are used to convert the data from and to the database into
the provided data model (StoreItem
). It allows to add some custom logic to the conversion.
It offers methods to observer changes in the collections document or in a specific document.
Additionally, you can use the get*
methods to just get the data without observing.
The rest of the methods are self-explanatory and you can check the comments there.
Firestore Rules
The firestore.rules
at the root of the project, defines the rules for the database.
Those are really important to ensure the data is protected and only available to authorized users.
The tipical rules is to check if the user is authorized to allow access. In addition you can check if the request comes from a specific user or query collections in the databsase to enable more complex logic.
You can also modify the rules from the Firebase Dashboard but we highly recommend using the firestore.rules
file.
To deploy the firestore.rules
and firestore.indexes.json
files, use:
Firestore Indexes
The firestore.indexes.json
file defines the indexes for the database. These indexes are requires to perform complex queries.
There are two main ways to create indexes:
-
Passive: whenever you perform a complex query that requires and index, the Firestore SDK will output a warning with a URL. Click on the URL to let the Dashboard create the index for you.
-
Active: modify the
firestore.indexes.json
file yourself adding the indexes you need and deploy them withfirebase deploy --only firestore
.
Troubleshooting
There is an issue with Firestore for iOS that requires to add this lines in the Podfile
Whenever you update firebase dependency. You must update the tag here and run pod update
from the app/ios
folder.