The syntax of List.getRange() method is:
Iterable<E> getRange(int start, int end)
This getRange() method of List creates an Iterable
that iterates over a range of elements.
Parameter | Optional/Required | Description |
---|---|---|
start | required | the starting index of the range |
end | required | the ending index of the range (exclusive) |
List.getRange() returns value of type Iterable<E>
.
In this example,
numbers
containing integers 1 to 5.getRange()
.void main() {
var numbers = [1, 2, 3, 4, 5];
var range = numbers.getRange(1, 4);
print(range.toList());
}
[2, 3, 4]
In this example,
characters
containing characters 'a' to 'e'.getRange()
.void main() {
var characters = ['a', 'b', 'c', 'd', 'e'];
var range = characters.getRange(2, 5);
print(range.toList());
}
[c, d, e]
In this example,
words
containing words 'apple' to 'eggplant'.getRange()
.void main() {
var words = ['apple', 'banana', 'cherry', 'date', 'eggplant'];
var range = words.getRange(0, 3);
print(range.toList());
}
[apple, banana, cherry]
In this Dart tutorial, we learned about getRange() method of List: the syntax and few working examples with output and detailed explanation for each example.