What is the List getRange() method in Dart?
The List getRange() method
In 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.
Syntax
List_name.getRange(start, end)
Parameters
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.
Return value
The getRange() method returns the items within the specified range. That is, (end - start).
Code
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);}
Explanation
- Line 2: We create a list named
colors. - Line 4: We use the
getRange()method with1and4as thestartandend, respectively. - Line 6: We print the result.
The getRange method skips start and takes end - start elements from the list.