contains method

Future<bool> contains(
  1. 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 this stream ends without finding a match, the future is completed with false.

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

Example:

final result = await Stream.fromIterable(['Year', 2021, 12, 24, 'Dart'])
    .contains('Dart');
print(result); // true

Implementation

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