# singleWhere method

Future<T> singleWhere (bool test(T element), { T orElse() })

Finds the single element in this stream matching `test`.

Like lastWhere, except that it is an error if more than one matching element occurs in this stream.

## Implementation

    Future<T> singleWhere(bool test(T element), {T orElse()}) {
      _Future<T> future = new _Future<T>();
      T result;
      bool foundResult = false;
      StreamSubscription subscription;
      subscription = this.listen(
          (T value) {
            _runUserCode(() => true == test(value), (bool isMatch) {
              if (isMatch) {
                if (foundResult) {
                  try {
                    throw IterableElementError.tooMany();
                  } catch (e, s) {
                    _cancelAndErrorWithReplacement(subscription, future, e, s);
                  }
                  return;
                }
                foundResult = true;
                result = value;
              }
            }, _cancelAndErrorClosure(subscription, future));
          },
          onError: future._completeError,
          onDone: () {
            if (foundResult) {
              future._complete(result);
              return;
            }
            try {
              if (orElse != null) {
                _runUserCode(orElse, future._complete, future._completeError);
                return;
              }
              throw IterableElementError.noElement();
            } catch (e, s) {
              _completeWithErrorCallback(future, e, s);
            }
          },
          cancelOnError: true);
      return future;
    }