whenAsync method

FutureOr<T?> whenAsync(
  1. Object? condition
)

Async-aware version of ObjectExtension.when.

Returns this FutureOr<T> if condition evaluates to true, otherwise null.

condition can be:

  • bool
  • bool Function() (lazy)
  • anything else → false

Implementation

FutureOr<T?> whenAsync(Object? condition) {
  final self = this;
  if (self is Future<T>) {
    return self.then((val) => _evalCondition(condition) ? val : null);
  } else {
    return _evalCondition(condition) ? self : null;
  }
}