Observer<T> class abstract

Observer for StreamUseCase callback-based listening.

Observer provides a callback-based interface for handling stream emissions from a StreamUseCase. It's optional — you can also use the stream directly with .listen() or await for.

When to Use Observer

  • When you prefer callback-based patterns
  • When integrating with legacy code that expects callbacks
  • When you need named callbacks for clarity

When to Use Stream Directly

  • When you want to use stream operators (map, where, etc.)
  • When using async/await patterns
  • When composing multiple streams

Example with Observer

class ProductListObserver extends Observer<List<Product>> {
  final void Function(List<Product>) onUpdate;
  final void Function(AppFailure) onFailed;

  ProductListObserver({
    required this.onUpdate,
    required this.onFailed,
  });

  @override
  void onData(List<Product> data) => onUpdate(data);

  @override
  void onError(AppFailure failure) => onFailed(failure);

  @override
  void onDone() => print('Stream completed');
}

// Usage
final observer = ProductListObserver(
  onUpdate: (products) => state.products = products,
  onFailed: (failure) => state.error = failure,
);

watchProductsUseCase.observe('electronics', observer);

Example with Stream (Preferred)

watchProductsUseCase('electronics').listen((result) {
  result.fold(
    (products) => state.products = products,
    (failure) => state.error = failure,
  );
});
Implementers

Constructors

Observer()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onData(T data) → void
Called when a new value is emitted.
onDone() → void
Called when the stream completes.
onError(AppFailure failure) → void
Called when an error occurs.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited