Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

What is the List getRange() method in Dart?

Maria Elijah

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

dart
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring