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 colors
var myRange = colors.getRange(1, 4).toList();
// display result
print(myRange);
}

Explanation

  • Line 2: We create a list named colors.
  • Line 4: We use the getRange() method with 1 and 4 as the start and end, respectively.
  • Line 6: We print the result.

The getRange method skips start and takes end - start elements from the list.

Free Resources