stream property

Stream<T>? get stream

The Stream currently being listened to.

Implementation

Stream<T>? get stream => _stream;
set stream (Stream<T>? stream)

Sets the Stream for this AsyncNotifier, updating its state.

Unsubscribes from any existing stream and subscribes to the new one.

  • On start: Updates snapshot to 'waiting'.
  • On new data: Updates snapshot with active state and data.
  • On error: Updates snapshot with error.
  • On done: Updates snapshot to done state.

No action is taken if the new stream is identical to the current one.

Example:

final _user = AsyncNotifier<User>();
_user.stream = someUserStream;

Implementation

set stream(Stream<T>? stream) {
  if (_stream == stream) return;
  if (stream == null) return cancel();
  _unsubscribe();

  _stream = stream;
  value = snapshot.inState(ConnectionState.waiting);

  _subscription = _stream?.listen(
    cancelOnError: cancelOnError,
    (data) {
      value = AsyncSnapshot.withData(ConnectionState.active, data);
      onData(data);
    },
    onError: (Object e, StackTrace s) {
      value = AsyncSnapshot.withError(ConnectionState.active, e, s);
      onError(e, s);
    },
    onDone: () {
      value = snapshot.inState(ConnectionState.done);
      onDone();
    },
  );
}