onStream<T> method
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 tobody
: Function to call with the next stream eventtag
: Optional tag for debuggingif_
: 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:
- onStreamDone - Only handle completion
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;
}