getRange method
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 ≤ start
≤ end
≤ length.
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).
final colors = <String>['red', 'green', 'blue', 'orange', 'pink'];
final firstRange = colors.getRange(0, 3);
print(firstRange.join(', ')); // red, green, blue
final secondRange = colors.getRange(2, 5);
print(secondRange.join(', ')); // blue, orange, pink
Implementation
@override
Iterable<E> getRange(int start, int end) {
if (start < 0 || start > _length || end < 0 || end > _length || start > end) {
throw RangeError.range(start, 0, _length, 'start');
}
final result = LinkedList<E>();
_Node<E>? current = _getNodeAt(start);
for (int i = start; i < end; i++) {
result.add(current!.value);
current = current.next;
}
return result;
}