TurboCollectionService<T extends TurboWriteableId<String> , API extends TurboFirestoreApi<T> > class
abstract
A service for managing a collection of Firestore documents with synchronized local state.
The TurboCollectionService provides a robust foundation for managing collections of documents that need to be synchronized between Firestore and local state. It handles:
- Local state management with optimistic updates
- Remote state synchronization
- Batch operations
- Transaction support
- Error handling
- Automatic user authentication state sync
Type Parameters:
T
- The document type, must extend TurboWriteableId<String>API
- The Firestore API type, must extend TurboFirestoreApi<T>
Example:
class UserService extends TurboCollectionService<User, UserApi> {
UserService({required super.api});
Future<void> updateUserName(String userId, String newName) async {
final user = findById(userId);
final updated = user.copyWith(name: newName);
await updateDoc(doc: updated);
}
}
Features:
- Automatic local state updates before remote operations
- Optimistic UI updates with rollback on failure
- Batch operations for multiple documents
- Transaction support for atomic operations
- Automatic stream update blocking during mutations
- Error handling and logging
- User authentication state synchronization
- Inheritance
-
- Object
- TurboAuthSyncService<
List< T> > - TurboCollectionService
- Implementers
Constructors
- TurboCollectionService.new({required API api})
- Creates a new TurboCollectionService instance.
Properties
- analytics → Analytics
-
Provides the configured
Analytics
functionality through theLoglytics
mixin per type ofD
.latefinalinherited - api → API
-
The Firestore API instance used for remote operations.
final
- cachedUserId ↔ String?
-
The ID of the currently authenticated user.
getter/setter pairinherited
-
docsPerId
→ ValueListenable<
Map< String, T> > -
Value listenable for the document collection state.
no setter
-
docsPerIdInformer
→ Informer<
Map< String, T> > -
Local state for documents, indexed by their IDs.
final
- hasDocs → bool
-
Whether the collection has any documents.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- isReady → Future
-
Future that completes when the service is ready to use.
no setter
- listenable → Listenable
-
Listenable for the document collection state.
no setter
- location → String
-
Used to define the location of Loglytics logging and implementation.
no setterinherited
- log → Log
-
Used to provide all logging capabilities.
latefinalinherited
-
onAuth
↔ FutureOr<
void> Function(User user)? -
Called when a user is authenticated.
getter/setter pairinherited
-
onData
→ Future<
void> Function(List<T> ? value, User? user) -
Handles data updates from the Firestore stream.
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
stream
→ FutureOr<
Stream< Function(User user)List< >T> ?> -
Returns a stream of data for the authenticated user.
no setterinherited
Methods
-
analyticsAs<
A extends Analytics> () → A -
Provides the configured
Analytics
functionality through theLoglytics
mixin per type ofA
.inherited -
createDoc(
{Transaction? transaction, required CreateDocDef< T> doc, bool doNotifyListeners = true}) → Future<TurboResponse< T> > - Creates a new document both locally and in Firestore.
-
createDocs(
{Transaction? transaction, required List< CreateDocDef< docs, bool doNotifyListeners = true}) → Future<T> >TurboResponse< List< >T> > - Creates multiple documents both locally and in Firestore.
-
createLocalDoc(
{required CreateDocDef< T> doc, bool doNotifyListeners = true}) → T - Creates a new document in local state.
-
createLocalDocs(
{required List< CreateDocDef< docs, bool doNotifyListeners = true}) → List<T> >T> - Creates multiple new documents in local state.
-
deleteDoc(
{required String id, bool doNotifyListeners = true, Transaction? transaction}) → Future< TurboResponse> - Deletes a document both locally and from Firestore.
-
deleteDocs(
{Transaction? transaction, required List< String> ids, bool doNotifyListeners = true}) → Future<TurboResponse> - Deletes multiple documents both locally and from Firestore.
-
deleteLocalDoc(
{required String id, bool doNotifyListeners = true}) → void - Deletes a document from local state.
-
deleteLocalDocs(
{required List< String> ids, bool doNotifyListeners = true}) → void - Deletes multiple documents from local state.
-
dispose(
) → Future< void> -
Disposes of the service by cleaning up resources.
override
-
exists(
String id) → bool - Whether a document with the given ID exists.
-
findById(
String id) → T - Finds a document by its ID. Throws if not found.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onDone(
int nrOfRetry, int maxNrOfRetry) → void -
Called when the stream is done.
inherited
-
onError(
TurboFirestoreException error) → void -
Called when a stream error occurs.
override
-
rebuild(
) → void - Forces a rebuild of the local state.
-
resetAndTryInitialiseStream(
) → Future< void> -
Resets and reinitialized the stream.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
tryFindById(
String? id) → T? - Finds a document by its ID. Returns null if not found.
-
tryHandleFirebaseAuthException<
T> ({required FirebaseAuthException firebaseAuthException, required Log log}) → TurboResponse< T> -
Handles Firebase Authentication exceptions and converts them to
TurboResponse
.inherited -
tryInitialiseStream(
) → Future< void> -
Initializes the authentication state stream and data synchronization.
inherited
-
turboVars<
V extends TurboAuthVars> ({String? id}) → V -
Returns a new instance of
V
with basic variables filled in. -
updateDoc(
{Transaction? transaction, required String id, required UpdateDocDef< T> doc, TurboWriteable remoteUpdateRequestBuilder(T doc)?, bool doNotifyListeners = true}) → Future<TurboResponse< T> > - Updates a document both locally and in Firestore.
-
updateDocs(
{Transaction? transaction, required List< String> ids, required UpdateDocDef<T> doc, bool doNotifyListeners = true}) → Future<TurboResponse< List< >T> > - Updates multiple documents both locally and in Firestore.
-
updateLocalDoc(
{required String id, required UpdateDocDef< T> doc, bool doNotifyListeners = true}) → T - Updates an existing document in local state.
-
updateLocalDocs(
{required List< String> ids, required UpdateDocDef<T> doc, bool doNotifyListeners = true}) → List<T> - Updates multiple existing documents in local state.
-
upsertDoc(
{Transaction? transaction, required String id, required UpsertDocDef< T> doc, TurboWriteable remoteUpdateRequestBuilder(T doc)?, bool doNotifyListeners = true}) → Future<TurboResponse< T> > - Upserts (updates or inserts) a document both locally and in Firestore.
-
upsertDocs(
{Transaction? transaction, required List< String> ids, required UpsertDocDef<T> doc, bool doNotifyListeners = true}) → Future<TurboResponse< List< >T> > - Upserts (updates or inserts) multiple documents both locally and in Firestore.
-
upsertLocalDoc(
{required String id, required UpsertDocDef< T> doc, bool doNotifyListeners = true}) → T - Upserts (updates or inserts) a document in local state.
-
upsertLocalDocs(
{required List< String> ids, required UpsertDocDef<T> doc, bool doNotifyListeners = true}) → List<T> - Upserts (updates or inserts) multiple documents in local state.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited