List
getRange()
methodIn Dart, the getRange()
method can be used to access elements in a list. This method defines a range to get a collection of items from a list.
List_name.getRange(start, end)
start
(required): Specifies the start position.end
(required): Specifies the end position.The start and end positions in the range must be valid at the time of the call. The valid range is one in which 0 ≤ start ≤ end ≤ length. An empty range with end == start is valid.
The getRange()
method returns the items within the specified range. That is, (end
- start
).
The following code exhibits the use of the getRange()
method in Dart.
void main(){List colors = ['blue', 'black', 'green', 'white', 'orange', 'pink'];// get items from list colorsvar myRange = colors.getRange(1, 4).toList();// display resultprint(myRange);}
colors
.getRange()
method with 1
and 4
as the start
and end
, respectively.The getRange
method skips start
and takes end - start
elements from the list.