Dart Iterable  retainWhere():

Dart Iterable retainWhere():

·

1 min read

retainWhere():

  • Removes all objects from this list that fail to satisfy test.

  • An object o satisfies test if test(o) is true.

  • This function takes element as a parameter and removes the rest of the items that do not specify the condition.

Example:

void main() {
  List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

  // Remove all odd numbers from the list
  numbers.retainWhere((number) => number.isEven);

  print(numbers); // Output: [2, 4, 6, 8, 10]
}
[2, 4, 6, 8, 10]

Exited.