watchById method

Stream<T?>? watchById(
  1. String id,
  2. String? userId
)

Watches a single entity by its ID, emitting the item on change or null if deleted. Returns null if the adapter does not support reactive queries.

Implementation

Stream<T?>? watchById(String id, String? userId) {
  _ensureInitialized();
  final adapterStream = localAdapter.watchById(id, userId: userId);
  if (adapterStream == null) return null;

  return adapterStream.asyncMap((item) async {
    if (item == null) {
      return null;
    }
    try {
      return await _applyPostFetchTransforms(item);
    } catch (e, stack) {
      _logger.error('Failed to apply post-fetch transforms to entity $id: $e', stack);
      return item; // Return original entity if transform fails
    }
  }).handleError((error, stack) {
    _logger.error('Error in watchById stream for $T:$id: $error', stack);
    // Don't rethrow - let the stream continue
  });
}