pipe<R> method

  1. @useResult
Future<R?> pipe<R>(
  1. FutureOr<R?> function(
    1. T value
    )
)

Returns a Future<R?> if this Future does not complete as null. Otherwise returns a Future that completes as null.

This function is similar to Maybe.bind which does not compose Futures.

Example:

Future<int?> computeAsync<T>(T value) async => 1;

Future<String?> foo = Future.value('value');
foo.pipe(computeAsync); // Future(1)

Future<String?> bar = null;
bar.pipe(computeAsync); // Future(null)

Chaining this function:

Future<int?> addAsync<T>(T value) async => value + 1;

Future<int?> one = Future.value(1);

one.pipe(addAsync).pipe(addAsync); // Future(3)

Implementation

@useResult Future<R?> pipe<R>(FutureOr<R?> Function(T value) function) async {
  final value = await this;
  return value == null ? null : function(value);
}