splice method

List<T> splice(
  1. int position, [
  2. int? removeCount,
  3. List<T>? value
])

Same as in JavaScript

[Negative Indexs are accepted]

Implementation

List<T> splice(int position, [int? removeCount, List<T>? value]) {
  if (position < 0) {
    position += length;
    if (position < 0) {
      position = 0;
    }
  }
  if (position > length) {
    position = length;
  }

  // start removing
  if (position < length) {
    if (removeCount == null) {
      // remove everything after the position
      removeRange(position, length);
    } else {
      if (removeCount > 0) {
        int temp = 1;
        while (temp <= removeCount && position < length) {
          removeAt(position);
          temp += 1;
        }
      }
    }
  }

  //start adding
  if (value != null && value.isNotEmpty) {
    List<T> temp = List<T>.from(value);
    while (temp.isNotEmpty) {
      insert(position, temp.removeLast());
    }
  }
  return this;
}