current property

  1. @override
T get current
override

The current element.

If the iterator has not yet been moved to the first element (moveNext has not been called yet), or if the iterator has been moved past the last element of the Iterable (moveNext has returned false), then current is unspecified. An Iterator may either throw or return an iterator specific default value in that case.

The current getter should keep its value until the next call to moveNext, even if an underlying collection changes. After a successful call to moveNext, the user doesn't need to cache the current value, but can keep reading it from the iterator.

final colors = ['blue', 'yellow', 'red'];
var colorsIterator = colors.iterator;
while (colorsIterator.moveNext()) {
  print(colorsIterator.current);
}

The output of the example is:

blue
yellow
red

Implementation

@override
T get current {
  if (currentIndex >= 0 && currentIndex < length) {
    return this[currentIndex];
  }
  throw IndexError.withLength(currentIndex, length);
}