Skip to main content

avoid-collection-methods-with-unrelated-types

added in: 1.6.0
preset: recommended

Avoid using collection methods with unrelated types, such as accessing a map of integers using a string key.

⚙️ Config

Use strict configuration (default is true) to exclude dynamic and Object types.

dart_code_metrics:
...
rules:
...
- avoid-collection-methods-with-unrelated-types:
strict: false

Example

❌ Bad:

final map = Map<int, String>();
map["str"] = "value"; // LINT
var a = map["str"]; // LINT
map.containsKey("str"); // LINT
map.containsValue(42); // LINT
map.remove("str"); // LINT

Iterable<int>.empty().contains("str"); // LINT

List<int>().remove("str"); // LINT

final set = {10, 20, 30};
set.contains("str"); // LINT
set.containsAll(Iterable<String>.empty()); // LINT
set.difference(<String>{}); // LINT
primitiveSet.intersection(<String>{}); // LINT
set.lookup("str"); // LINT
primitiveList.remove("str"); // LINT
set.removeAll(Iterable<String>.empty()); // LINT
set.retainAll(Iterable<String>.empty()); // LINT

✅ Good:

final map = Map<int, String>();
map[42] = "value";
var a = map[42];
map.containsKey(42);
map.containsValue("value");
map.remove(42);

Iterable<int>.empty().contains(42);

List<int>().remove(42);

final set = {10, 20, 30};
set.contains(42);
set.containsAll(Iterable<int>.empty());
set.difference(<int>{});
primitiveSet.intersection(<int>{});
set.lookup(42);
primitiveList.remove(42);
set.removeAll(Iterable<int>.empty());
set.retainAll(Iterable<int>.empty());