getRange method Null safety

Iterable<E> getRange(
  1. int start,
  2. int end
)
override

Creates an Iterable that iterates over a range of elements.

The returned iterable iterates over the elements of this list with positions greater than or equal to start and less than end.

The provided range, start and end, must be valid at the time of the call. A range from start to end is valid if 0 ≤ startendlength. An empty range (with end == start) is valid.

The returned Iterable behaves like skip(start).take(end - start). That is, it does not break if this list changes size, it just ends early if it reaches the end of the list early (if end, or even start, becomes greater than length).

List<String> colors = ['red', 'green', 'blue', 'orange', 'pink'];
Iterable<String> range = colors.getRange(1, 4);
range.join(', ');  // 'green, blue, orange'
colors.length = 3;
range.join(', ');  // 'green, blue'

Implementation

Iterable<E> getRange(int start, int end) {
  RangeError.checkValidRange(start, end, this.length);
  return SubListIterable<E>(this, start, end);
}