current property

  1. @override
V current

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()) {

The output of the example is:



V get current {
  // Prior to NNBD, this returned null when iteration was complete. In order
  // to avoid a hard breaking change, we return "null as V" in that case so
  // that if strong checking is not enabled or V is nullable, the existing
  // behavior is preserved.
  if (_state == _walk && _current != null) {
    return _current?.object as V;
  return null as V;