Cloud Firestore
ShipFlutter uses Cloud Firestore as its database solution, providing a flexible, scalable NoSQL cloud database with offline support. The database module is part of the core Firebase integration, making it seamless to work with user data, settings, and app state.
Before starting, make sure you have completed the Firebase setup, including the Firestore setup steps.
Using FirebaseStore
The database functionality is implemented in the core/firebase/database
module, providing a type-safe wrapper around Firestore operations through the FirebaseStore
class:
Configuration
- root: The path to your collection (e.g.,
users
orstore/1234/purchases
) - fromFirestore: Convert Firestore data to your model
- toFirestore: Convert your model to Firestore data
Key Features
Security Rules
The firestore.rules
file at the root of your project defines access control:
Best Practices
-
Data Structure
- Keep documents small and flat
- Use subcollections for scalable relationships
- Follow the Firebase recommendations
-
Queries
- Create indexes for complex queries
- Use filters efficiently
- Limit result size when possible
- Cache frequently accessed data
-
Offline Support
- Enable persistence for offline access
- Handle offline/online state changes
- Use
onlyChanges
parameter to skip cached data - Implement retry logic for failed operations
Troubleshooting
Here are some common issues you might encounter:
iOS performance issues
For better performance on iOS, we use a pre-compiled version of Firestore by adding these lines to the Podfile
:
iOS build issues
Add these lines if you encounter iOS build issues: