stream method

  1. @override
Stream<T> stream(
  1. String id
)
override

Creates a real-time stream of changes for a specific document.

Initial Emission: Immediately emits existing data (BehaviorSubject-like). Error: Emits RepositoryException.notFound if document doesn't exist initially. Deletion Behavior: Stream closes when document is deleted.

Implementation

@override
Stream<T> stream(String id) {
  final controller = StreamController<T>();
  var hasEmitted = false;

  final sub = store.doc(_normaliseToFullPath(id)).snapshots().listen(
    (snapshot) {
      if (!snapshot.exists) {
        if (!hasEmitted) {
          controller.addError(RepositoryException.notFound(id));
        } else {
          unawaited(controller.close());
        }
        return;
      }

      hasEmitted = true;
      final data = snapshot.data()!;
      controller.add(fromFirestore(
        snapshot.reference,
        RepositoryFirestore.typeConversionFromFirebase.convert(source: data),
      ),);
    },
    onError: controller.addError,
  );

  controller.onCancel = sub.cancel;
  return controller.stream;
}