onStream<T> method

SelectBuilder<R> onStream<T>(
  1. Stream<T> stream,
  2. FutureOr<R> body(
    1. T
    ), {
  3. Object? tag,
  4. bool if_()?,
})

Race a Stream - waits for the next event.

Automatically cancels the stream subscription if another branch wins. Use this when you want to react to the next event from a stream.

Parameters:

  • stream: Stream to listen to
  • body: Function to call with the next stream event
  • tag: Optional tag for debugging
  • if_: Optional guard condition

Example:

await XSelect.run<String>((s) => s
  ..onStream(userClicks, (click) => 'User clicked: $click')
  ..onStream(keyboardEvents, (key) => 'Key pressed: $key')
  ..onStream(networkEvents, (event) => 'Network: $event')
  ..onTimeout(Duration(seconds: 30), () => 'No user activity')
);

See also:

Implementation

SelectBuilder<R> onStream<T>(Stream<T> stream, FutureOr<R> Function(T) body,
    {Object? tag, bool Function()? if_}) {
  _branches.add((StreamBranch<T, R>(stream, body, tag: tag), if_));
  return this;
}