FirebaseBackend class provides centralized access to Firebase services and utility methods for working with Firestore.
Source: lib/service/firebase_backend.dart
Static Getters
firestore
Access to the Firestore database instance.lib/service/firebase_backend.dart:8
storage
Access to Firebase Storage instance.lib/service/firebase_backend.dart:9
auth
Access to Firebase Authentication instance.lib/service/firebase_backend.dart:10
users
Reference to theusers collection.
lib/service/firebase_backend.dart:12
products
Reference to theproducts collection.
lib/service/firebase_backend.dart:15
events
Reference to theevents collection.
lib/service/firebase_backend.dart:18
reviews
Reference to thereviews collection.
lib/service/firebase_backend.dart:21
Methods
ensureInitialized
Verifies that Firebase has been initialized before performing operations.AppError if Firebase is not initialized
Source: lib/service/firebase_backend.dart:27
nextNumericId
Generates the next sequential numeric ID for a collection using atomic transactions.counterKey- The counter name (e.g., “products”, “users”, “orders”)
lib/service/firebase_backend.dart:37
This method uses Firestore transactions to ensure IDs are unique even under concurrent access. Counters are stored in
_meta/counters document.findByNumericId
Finds a document by its numericid field (not Firestore document ID).
collection- The collection to searchid- The numeric ID value
AppError with code firestore.not_found if no document found
Source: lib/service/firebase_backend.dart:52
findRefByNumericId
Finds a document reference by its numericid field.
collection- The collection to searchid- The numeric ID value
AppError if not found
Source: lib/service/firebase_backend.dart:67
ensureUserProfile
Creates or updates a user profile document based on Firebase Auth user.firebaseUser- The Firebase Authentication userfallbackName- Name to use if displayName is empty (optional)
- If profile exists by
firebase_uid, updates it with latest email/displayName - If profile doesn’t exist, creates new one with auto-generated numeric ID
- Assigns role 0 (standard user) by default
lib/service/firebase_backend.dart:75
normalizeSnapshotData
Extracts and normalizes data from a Firestore document snapshot.doc- Firestore document snapshot
- All document fields
doc_idfield containing the Firestore document ID- All
Timestampfields converted to ISO 8601 strings
lib/service/firebase_backend.dart:124
normalizeMap
Normalizes a map by converting Firestore types to JSON-friendly types.Timestamp→ ISO 8601 string- Nested maps → recursively normalized
- Iterables → normalized lists
lib/service/firebase_backend.dart:131
Error Handling
All methods throwAppError exceptions with specific error codes:
| Code | Description |
|---|---|
firebase.not_initialized | Firebase hasn’t been initialized |
firestore.not_found | Document not found by numeric ID |
firestore.permission_denied | Firestore security rules denied access |
firestore.failed_precondition | Firestore database not created in console |
firestore.unavailable | Network issues or project unavailable |
firestore.unauthenticated | Authentication required |
firestore.unknown | Usually indicates rules issues |
lib/service/firebase_backend.dart:157
Usage Example
The numeric ID system allows for easier API interoperability while still using Firestore’s auto-generated document IDs internally.