drop<T> function

List<T> drop<T>(
  1. List<T> list, [
  2. int n = 1
])

removes n number of elements from the beginning of list

// If used as function,
// it creates a new copy of the output and list's object is untouched
var list = <int>[1, 2, 3, 4, 5];
var newObject = drop(list); // newObject = [2, 3, 4, 5;

var newObject = drop(list, 3); // newObject = [4, 5];

var newObject = drop(list, 5); // newObject = []; // does not throw error :D

// If used as method, it directly alter the list's object

var list = <int>[1, 2, 3, 4, 5];
list.drop(); // list = [2, 3, 4, 5];

list.drop(3); // list = [5];

list.drop(5); // list = []; // does not throw error :D

Implementation

List<T> drop<T>(List<T> list, [int n = 1]) {
  if (n < 1) {
    return List<T>.from(list);
  }
  var length = list.length;
  var newObject = <T>[];
  for (var i = n; i < length; i++) {
    newObject.add(list[i]);
  }
  return newObject;
}