dropLast method

List<E> dropLast(
  1. int n
)

Returns a new list containing all elements except last n elements.

Implementation

List<E> dropLast(int n) {
  if (n < 0) {
    throw ArgumentError('Requested element count $n is less than zero.');
  }
  if (n == 0) toList();

  var resultSize = length - n;
  if (resultSize <= 0) return [];
  if (resultSize == 1) return [first];
  return sublist(0, length - n);
}