copyWithReplacedAt method

List<T> copyWithReplacedAt(
  1. int index,
  2. T newItem
)

Returns a copy with the element at index replaced

Implementation

List<T> copyWithReplacedAt(int index, T newItem) {
  if (index < 0 || index >= length) {
    throw RangeError('Index $index out of bounds for list of length $length');
  }
  return [...sublist(0, index), newItem, ...sublist(index + 1)];
}