W3cubDocs

/Dart 2

any method

Future<bool> any (bool test(T element))

Checks whether test accepts any element provided by this stream.

Calls test on each element of this stream. If the call returns true, the returned future is completed with true and processing stops.

If this stream ends without finding an element that test accepts, the returned future is completed with false.

If this stream emits an error, or if the call to test throws, the returned future is completed with that error, and processing stops.

Implementation

Future<bool> any(bool test(T element)) {
  _Future<bool> future = new _Future<bool>();
  StreamSubscription subscription;
  subscription = this.listen(
      (T element) {
        _runUserCode(() => test(element), (bool isMatch) {
          if (isMatch) {
            _cancelAndValue(subscription, future, true);
          }
        }, _cancelAndErrorClosure(subscription, future));
      },
      onError: future._completeError,
      onDone: () {
        future._complete(false);
      },
      cancelOnError: true);
  return future;
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.5.0/dart-async/Stream/any.html