slice method

List<T> slice(
  1. int? start, [
  2. int? end
])

Same as in JavaScript

[Negative Indexs are accepted]

slices the list

Implementation

List<T> slice(int? start, [int? end]) {
  start ??= 0;

  if (start < 0) {
    start += length;
    if (start < 0) {
      start = 0;
    }
  }
  if (start >= length) {
    return <T>[];
  }

  end ??= length;

  if (end < 0) {
    end += length;
    if (end < 0) {
      end = 0;
    }
  }
  if (end > length) {
    end = length;
  }
  if (start > end) {
    return <T>[];
  }
  var out = List<T>.filled(end - start, this[0]);
  while (end != null && start <= --end) {
    out[end - start] = this[end];
  }
  return out;
}