splitBeforeIndex method Null safety

List<List<E>> splitBeforeIndex(
  1. int index
)

Returns a list of two sub-lists based off self: List 1: All items before specified index List 2: All items after specified index Negative [-1] is the same as the last list item and all negative numbers are likewise 0 leads to [[],[this]] whereas an index>this.length leads to [this,[]]

Implementation

List<List<E>> splitBeforeIndex(int index) {
  while (index < 0) {
    index += length;
  }
  List<List<E>> newList = [[], []];
  for (int i in range(length)) {
    if (i >= index) {
      newList[1].add(this[i]);
    } else {
      newList[0].add(this[i]);
    }
  }
  return newList;
}