fromCallable<T> static method

Stream<T> fromCallable<T>(
  1. FutureOr<T> callable(), {
  2. bool reusable = false,
})

Returns a Stream that, when listening to it, calls a function you specify and then emits the value returned from that function.

If result from invoking callable function:

  • Is a Future: when the future completes, this stream will fire one event, either data or error, and then close with a done-event.
  • Is a T: this stream emits a single data event and then completes with a done event.

By default, a FromCallableStream is a single-subscription Stream. However, it's possible to make them reusable. This Stream is effectively equivalent to one created by (() async* { yield await callable() }()) or (() async* { yield callable(); }()).

ReactiveX doc

Example

Rx.fromCallable(() => 'Value').listen(print); // prints Value

Rx.fromCallable(() async {
  await Future<void>.delayed(const Duration(seconds: 1));
  return 'Value';
}).listen(print); // prints Value

Implementation

static Stream<T> fromCallable<T>(FutureOr<T> Function() callable,
        {bool reusable = false}) =>
    FromCallableStream(callable, reusable: reusable);