description

# elementAt method

Future<T> elementAt(

1. int index

)

Returns the value of the `index`th data event of this stream.

Stops listening to this stream after the `index`th data event has been received.

Internally the method cancels its subscription after these elements. This means that single-subscription (non-broadcast) streams are closed and cannot be reused after a call to this method.

If an error event occurs before the value is found, the future completes with this error.

If a done event occurs before the value is found, the future completes with a RangeError.

## Implementation

    Future<T> elementAt(int index) {
      RangeError.checkNotNegative(index, "index");
      @pragma('vm:awaiter-link')
      _Future<T> result = _Future<T>();
      int elementIndex = 0;
      StreamSubscription<T> subscription;
      subscription = this.listen(
        null,
        onError: result._completeError,
        onDone: () {
          result._completeError(
            IndexError.withLength(
              index,
              elementIndex,
              indexable: this,
              name: "index",
            ),
            StackTrace.current,
          );
        },
        cancelOnError: true,
      );
      subscription.onData((T value) {
        if (index == elementIndex) {
          _cancelAndValue(subscription, result, value);
          return;
        }
        elementIndex += 1;
      });

      return result;
    }