copyWithReplaced method

List<T> copyWithReplaced(
  1. T oldItem,
  2. T newItem
)

Returns a copy with the first occurrence of oldItem replaced

Implementation

List<T> copyWithReplaced(T oldItem, T newItem) {
  final index = indexOf(oldItem);
  if (index == -1) return List.of(this);
  return [...sublist(0, index), newItem, ...sublist(index + 1)];
}