W3cubDocs

/Dart 2

contains method

Future<bool> contains (Object needle)

Returns whether needle occurs in the elements provided by this stream.

Compares each element of this stream to needle using Object.==. If an equal element is found, the returned future is completed with true. If the stream ends without finding a match, the future is completed with false.

If the stream contains an error, or the call to Object.== throws, the returned future is completed with that error, and processing stops.

Implementation

Future<bool> contains(Object needle) {
  _Future<bool> future = new _Future<bool>();
  StreamSubscription subscription;
  subscription = this.listen(
      (T element) {
        _runUserCode(() => (element == needle), (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.dartlang.org/stable/2.0.0/dart-async/Stream/contains.html