onData property

  1. @override
Future<void> Function(List<DTO>? value, User? user) get onData
override

Handles incoming data updates from Firestore with pre and post-sync notifications.

This callback is triggered when:

  • New document data is received from Firestore
  • The user's authentication state changes

The method:

  • Notifies before sync via beforeSyncNotifyUpdate if user is authenticated
  • Updates local state with new document data
  • Marks the service as ready after first update
  • Notifies after sync via afterSyncNotifyUpdate
  • Clears local state if user is not authenticated

Parameters:

  • value - The new document values from Firestore
  • user - The current Firebase user

Implementation

@override
Future<void> Function(List<DTO>? value, User? user) get onData {
  return (value, user) async {
    final docs = value ?? defaultValues();
    if (user != null) {
      log.debug('Updating docs for user ${user.uid}');
      await beforeSyncNotifyUpdate(docs);
      docsNotifier.update(
        TModelDocs.fromDtos(
          dtos: docs,
          modelBuilder: (dto) => modelBuilder(this, null, dto),
          sortFilteredListsMap: initialSortFilteredListsMap?.call(),
        ),
      );
      markAsReady();
      await afterSyncNotifyUpdate(docs);
      log.debug('Updated ${docs.length} docs');
    } else {
      log.debug('User is null, clearing docs');
      await beforeSyncNotifyUpdate([]);
      resetLocalDocs();
      await afterSyncNotifyUpdate([]);
    }
  };
}