Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:async
  3. Stream<T>
  4. elementAt method
elementAt
description

elementAt method

Future<T>elementAt(
  1. intindex
)

Returns the value of theindexth data event of this stream.

Stops listening to this stream after theindexth data event has beenreceived.

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

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

If a done event occurs before the value is found, the future completeswith aRangeError.

Implementation

Future<T> elementAt(int index) {  RangeError.checkNotNegative(index, "index");  _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.empty,      );    },    cancelOnError: true,  );  subscription.onData((T value) {    if (index == elementIndex) {      _cancelAndValue(subscription, result, value);      return;    }    elementIndex += 1;  });  return result;}
  1. Dart
  2. dart:async
  3. Stream<T>
  4. elementAt method
Stream class

[8]ページ先頭

©2009-2025 Movatter.jp