Range constructor
Range constructor
Create a range that begins at start. By default, start is 0.
Ordinarily, the Range will end at stop, exclusive of stop.
If stopExclusive is false, the value of stop is included in the range.
The step determines the amount by which the range progresses on each
iteration. By default step is 1.
final r = range(stop: 10, step: 2);
print(r.toList());
Result: [0, 2, 4, 6, 8]
step is always a positive number - an ArgumentError is thrown
if step is <= 0.
If the range is moving backwards (e.g. 10 -> 0), Range will correctly step in the reverse direction.
For example, start at 10 and stop before 0, with a step of 2:
final r = range(start: 10, stop: 0, step: 2);
print(r.toList());
Result: [10, 8, 6, 4, 2]
To get to 0, set stopExclusive to false:
final r = range(start: 10, stop: 0, step: 2, stopExclusive: false);
print(r.toList());
Result: [10, 8, 6, 4, 2, 0]
Implementation
Range({
this.start = 0,
required this.stop,
this.step = 1,
this.stopExclusive = true,
}) {
if (step <= 0) throw ArgumentError.value(step, 'step');
}