Skip to content

avoid_map_keys_contains

v0.4.0 Warning Fix Collection & Type

Using map.keys.contains(key) iterates through all keys to check for existence, while map.containsKey(key) performs a direct hash lookup. This rule catches the slower pattern and suggests the more efficient alternative.

Map.keys returns an Iterable that must be traversed linearly to check for a key, making it O(n). Map.containsKey() uses the map’s hash table directly and runs in O(1). For large maps, the performance difference is significant.

See also: Map.containsKey

void example() {
final map = {'lat': 52.2, 'lon': 21.0};
// Use containsKey() instead
final exists = map.keys.contains('lat');
// Also in conditions
if (map.keys.contains('foo')) {
print('found');
}
}
void example() {
final map = {'lat': 52.2, 'lon': 21.0};
final exists = map.containsKey('lat');
if (map.containsKey('foo')) {
print('found');
}
}

This rule is in the recommended preset, so it is on with preset: recommended or preset: opinionated. Add it to preset: core with avoid_map_keys_contains: true.

To turn it off:

many_lints.yaml
rules:
avoid_map_keys_contains: false

To keep the rule on but skip certain paths, use per-rule exclude.