copyWithReplacedWhere method

List<T> copyWithReplacedWhere(
  1. bool test(
    1. T item
    ),
  2. T newItem
)

Returns a copy with the first element matching test replaced

Implementation

List<T> copyWithReplacedWhere(bool Function(T item) test, T newItem) {
  final index = indexWhere(test);
  if (index == -1) return List.of(this);
  return [...sublist(0, index), newItem, ...sublist(index + 1)];
}