W3cubDocs

/Dart 2

sublist method

List<E> sublist (int start, [ int end ])
override

Returns a new list containing the elements between start and end.

The new list is a List<E> containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.

var colors = ["red", "green", "blue", "orange", "pink"];
print(colors.sublist(1, 3)); // [green, blue]

If end is omitted, it defaults to the length of this list.

print(colors.sublist(1)); // [green, blue, orange, pink]

The start and end positions must satisfy the relations 0 ≤ startendthis.length If end is equal to start, then the returned list is empty.

Implementation

List<E> sublist(int start, [int end]) {
  int listLength = this.length;
  end ??= listLength;
  RangeError.checkValidRange(start, end, listLength);
  int length = end - start;
  List<E> result = <E>[]..length = length;
  for (int i = 0; i < length; i++) {
    result[i] = this[start + i];
  }
  return result;
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.5.0/dart-collection/ListMixin/sublist.html