contains_on_map_keys
Reports map.keys.contains(key). Write map.containsKey(key).
- Tier
- advisory
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- writes
containsKey, keeping a?.beforekeys
What it reports
Section titled “What it reports”map.keys.contains(key) with one argument, on a value whose static type is a Map of dart:core or implements it, read with . or ?..
It does not report:
values.contains;containson a list or a set;keysof a class that is no map;- a cascade.
No fix is offered where a comment stands between keys and contains.
The contract of Map.keys is that its contains answers through the map’s containsKey, and the core maps keep it. So this is about saying what you mean, not about speed: containsKey asks the map directly whether it holds the key.
Example
Section titled “Example”bool has(Map<String, int> prices, String name) => prices.keys.contains(name);bool has(Map<String, int> prices, String name) => prices.containsKey(name);Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// The key set is the one under test here, not the map.// ignore: assay/contains_on_map_keys// ignore_for_file: assay/contains_on_map_keys silences it in a whole file, and // ignore: contains_on_map_keys without the prefix works too. See Suppressions.