TDocService<DTO extends TWriteableId, MODEL extends TModel<DTO> > class
A service for managing a single Firestore document with synchronized local state.
Extends TAuthSyncService to provide functionality for managing a single document that needs to be synchronized between Firestore and local state. It handles:
- Local state management with optimistic updates
- Remote state synchronization
- Transaction support
- Error handling
- Automatic user authentication state sync
- Before/after update notifications
Type Parameters:
DTO- The document type, must extendTWriteableId
- Inheritance
-
- Object
- TAuthSyncService<
DTO> - TDocService
- Implementers
Constructors
-
TDocService({required TDocValueBuilderDef<
DTO, MODEL> defaultValue, required TDocModelBuilderDef<DTO, MODEL> modelBuilder, TDocApiBuilderDef<DTO, MODEL> ? apiBuilder, TDocStreamBuilderDef<DTO, MODEL> ? streamBuilder, required TFirestoreCollection<DTO> collection, bool initialiseStream = TFirestoreApiDefaults.initialiseStream, TDocValueBuilderDef<DTO, MODEL> ? initialValue, IFirestoreCacheService? firestoreCacheService, ValueChanged<DTO?> ? afterLocalNotifyUpdate, ValueChanged<DTO?> ? beforeLocalNotifyUpdate, TDocValueBuilderDef<DTO, MODEL> ? onMissingRemoteValue, List<Future> readyDeps(User user)?}) - Creates a new TDocService instance.
Properties
-
afterLocalNotifyUpdate
↔ ValueChanged<
DTO?> ? -
Called after local state is updated.
getter/setter pair
- analytics → TAnalytics
-
Provides the configured
TAnalyticsfunctionality through theTurbolyticsmixin per type ofD.latefinalinherited -
api
→ TFirestoreApi<
DTO> -
The Firestore API instance for remote operations.
latefinal
-
apiBuilder
→ TDocApiBuilderDef<
DTO, MODEL> ? -
Optional builder function to create the Firestore API instance. If not provided, the API will be created using the collection's
api()method.final -
beforeLocalNotifyUpdate
↔ ValueChanged<
DTO?> ? -
Called before local state is updated.
getter/setter pair
-
collection
→ TFirestoreCollection<
DTO> -
The Firestore collection definition that this service manages.
final
-
defaultValue
→ TDocValueBuilderDef<
DTO, MODEL> -
Function to provide default document value.
final
-
doc
→ ValueListenable<
MODEL> -
Value listenable for the document state.
no setter
- firestoreCacheService → IFirestoreCacheService?
-
Optional Firestore cache service for caching document data locally.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- id → String
-
Whether a document exists in local state.
The document ID.
no setter
-
initialValue
→ TDocValueBuilderDef<
DTO, MODEL> ? -
Function to provide initial document value.
final
-
isReady
→ Future<
void> -
Future that completes when the service is ready.
no setter
- listenable → Listenable
-
Listenable for the document state.
no setter
- location → String
-
Used to define the location of Turbolytics logging and implementation.
no setterinherited
- log → TLog
-
Used to provide all logging capabilities.
latefinalinherited
-
modelBuilder
→ TDocModelBuilderDef<
DTO, MODEL> -
Function to convert between DTO and MODEL for local state management.
final
- onAuth ↔ FutureOr Function(User user)?
-
Called when a user is authenticated.
getter/setter pairinherited-setteroverride-getter
-
onData
→ Future<
void> Function(DTO? value, User? user) -
Handles incoming data updates from Firestore.
no setteroverride
-
onMissingRemoteValue
→ TDocValueBuilderDef<
DTO, MODEL> ? -
Whether to attempt to create a missing remote document if the local state is default and no remote document exists.
final
-
readyDeps
→ List<
Future> Function(User user)? -
List of dependencies to wait for before anything else.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
stream
→ Stream<
DTO?> Function(User user) -
Returns a stream of data for the authenticated user.
no setteroverride
-
streamBuilder
→ TDocStreamBuilderDef<
DTO, MODEL> ? -
Optional builder function to create the Firestore stream. If not provided, the stream will be created using the API's
streamAllWithConverter()method.final - user ↔ User?
-
The currently authenticated Firebase user.
getter/setter pairinherited
- userId → String?
-
The ID of the currently authenticated user.
no setterinherited
Methods
-
analyticsAs<
A extends TAnalytics> () → A -
Provides the configured
TAnalyticsfunctionality through theTurbolyticsmixin per type ofA.inherited -
clearLocalDoc(
{bool doNotifyListeners = true}) → void - Clears the local document state.
-
createDoc(
{Transaction? transaction, String? id, required CreateDocDef< DTO, MODEL> doc, bool doNotifyListeners = true}) → Future<TurboResponse< DTO> > - Creates a new document both locally and in Firestore.
-
createLocalDoc(
{String? id, required CreateDocDef< DTO, MODEL> doc, bool doNotifyListeners = true}) → DTO - Creates a new document in local state.
-
defaultDoc(
) → MODEL -
defaultDto(
) → DTO -
deleteDoc(
{required String id, bool doNotifyListeners = true, Transaction? transaction}) → Future< TurboResponse< void> > - Deletes a document both locally and from Firestore.
-
deleteLocalDoc(
{required String id, bool doNotifyListeners = true}) → void - Deletes a document from local state.
-
dispose(
) → Future< void> -
Disposes of the document service and releases resources.
override
-
getCustomClaim<
T> (String key, {T? defaultValue}) → T? -
Returns a specific custom claim by key.
inherited
-
getCustomClaims(
) → Map< String, dynamic> ? -
Returns all custom claims from the cached token.
inherited
-
getTokenResult(
) → Future< IdTokenResult?> -
Returns the cached token result with claims.
inherited
-
handleMissingRemoteValue(
) → Future< void> -
initialDoc(
) → MODEL -
initialDto(
) → DTO -
markAsReady(
) → void - Marks the service as ready by completing the ready state.
-
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(
TFirestoreException 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
-
tryHandleFirebaseAuthException<
T> ({required FirebaseAuthException firebaseAuthException, required TLog 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
-
updateDoc(
{Transaction? transaction, required String id, required UpdateDocDef< DTO, MODEL> doc, TWriteable remoteUpdateRequestBuilder(DTO doc)?, bool doNotifyListeners = true}) → Future<TurboResponse< DTO> > - Updates a document both locally and in Firestore.
-
updateLocalDoc(
{required String id, required UpdateDocDef< DTO, MODEL> doc, bool doNotifyListeners = true}) → DTO - Updates an existing document in local state.
-
upsertDoc(
{Transaction? transaction, required String id, required UpsertDocDef< DTO, MODEL> doc, TWriteableId remoteUpdateRequestBuilder(DTO doc)?, bool doNotifyListeners = true}) → Future<TurboResponse< DTO> > - Upserts (updates or inserts) a document both locally and in Firestore.
-
upsertLocalDoc(
{required String id, required UpsertDocDef< DTO, MODEL> doc, bool doNotifyListeners = true}) → DTO - Upserts (updates or inserts) a document in local state.
-
vars<
V extends TVars> ({String? id}) → V -
Returns a new instance of
Vwith basic variables filled in.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited