replaceFirstWhere method

  1. @useResult
IList<T> replaceFirstWhere(
  1. bool test(
    1. T item
    ),
  2. T replacement(
    1. T? item
    ), {
  3. bool addIfNotFound = false,
})

Finds the first item that satisfies the provided test, and replace it with the result of replacement.

  • If addIfNotFound is false, return the unchanged list if no item satisfies the test.

  • If addIfNotFound is true, add the replacement to the end of the list if no item satisfies the test.

Implementation

@useResult
IList<T> replaceFirstWhere(
  bool Function(T item) test,
  T Function(T? item) replacement, {
  bool addIfNotFound = false,
}) {
  final int index = indexWhere(test);
  return (index != -1)
      ? put(index, replacement(this[index]))
      : addIfNotFound
          ? add(replacement(null))
          : this;
}