moveNext method

  1. @override
bool moveNext()
override

Advances the iterator to the next element of the iteration.

Should be called before reading current. If the call to moveNext returns true, then current will contain the next element of the iteration until moveNext is called again. If the call returns false, there are no further elements and current should not be used any more.

It is safe to call moveNext after it has already returned false, but it must keep returning false and not have any other effect.

A call to moveNext may throw for various reasons, including a concurrent change to an underlying collection. If that happens, the iterator may be in an inconsistent state, and any further behavior of the iterator is unspecified, including the effect of reading current.

final colors = ['blue', 'yellow', 'red'];
final colorsIterator = colors.iterator;
print(colorsIterator.moveNext()); // true
print(colorsIterator.moveNext()); // true
print(colorsIterator.moveNext()); // true
print(colorsIterator.moveNext()); // false

Implementation

@override
bool moveNext() {
  if (_length != _tree._length) {
    throw ConcurrentModificationError();
  }
  while (true) {
    if (_currentNode == null) {
      if (_tree._rootNode == null) return false;
      var _c = IteratorNode(_tree._rootNode!, null);
      while (_c.treeNode.left != null) {
        _c = IteratorNode(_c.treeNode.left!, _c);
      }
      _currentNode = _c;
      _currentNode?.currentIterated = true;
      return true;
    }

    if (_currentNode!.currentIterated) {
      if (_currentNode!.treeNode.right != null) {
        if (_currentNode!.rightIterated) {
          _currentNode = _currentNode!.parent;
          if (_currentNode == null) return false;
        } else {
          var _c = IteratorNode(_currentNode!.treeNode.right!, _currentNode);
          _currentNode!.rightIterated = true;
          while (_c.treeNode.left != null) {
            _c = IteratorNode(_c.treeNode.left!, _c);
          }
          _currentNode = _c;
          _currentNode?.currentIterated = true;
          return true;
        }
      } else {
        _currentNode = _currentNode!.parent;
        if (_currentNode == null) return false;
      }
    } else {
      _currentNode!.currentIterated = true;
      return true;
    }
  }
}