moveNext method Null safety

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.

Implementation

bool moveNext() {
  _position = _nextPosition;
  if (_position == string.length) {
    _currentCodePoint = -1;
    return false;
  }
  int codeUnit = string.codeUnitAt(_position);
  int nextPosition = _position + 1;
  if (_isLeadSurrogate(codeUnit) && nextPosition < string.length) {
    int nextCodeUnit = string.codeUnitAt(nextPosition);
    if (_isTrailSurrogate(nextCodeUnit)) {
      _nextPosition = nextPosition + 1;
      _currentCodePoint = _combineSurrogatePair(codeUnit, nextCodeUnit);
      return true;
    }
  }
  _nextPosition = nextPosition;
  _currentCodePoint = codeUnit;
  return true;
}