retainWhere method

void retainWhere(
  1. bool predicate(
    1. K key,
    2. V value
    )
)

Retains all entries that satisfy the given predicate.

final foo = {'a': 1, 'b': 2};
foo.retainWhere((k, v) => v < 2); // {'b': 2}

Implementation

void retainWhere(bool Function(K key, V value) predicate) {
  // We remove all elements at the end to ensure atomicity.
  [ for (final MapEntry(:key, :value) in entries) if (!predicate(key, value)) key ].forEach(remove);
}