range function Null safety

Iterable<int> range(
  1. int stop,
  2. {int? start,
  3. int? step}
)

Similar to python range function iterates from start up to yet not including stop, incrementing by every step. Ex: for (int i in range(5, start: 1, step: 2) {print(i);}

1 3

Implementation

Iterable<int> range(int stop, {int? start, int? step}) sync* {
  step ??= 1;
  start ??= 0;
  if (step == 0) {
    throw ArgumentError('Step is 0, can\'t iterate');
  }
  if (stop == start) {
    throw ArgumentError('Start $start is == stop $stop');
  } else if (stop > start && step.isNegative) {
    throw ArgumentError('Start $start is >'
        ' than stop $stop, yet step $step is positive');
  } else if (stop < start && step.isPositive) {
    throw ArgumentError('Start $start is <'
        ' than stop $stop, yet step $step is negative');
  }

  for (int i = start; i < stop; i += step) {
    yield i;
  }
}