The Dart collections API has methods that allow developers to overcome type-safety restriction of the parameter, such as
Iterable.contains
.
When the actual type of the object provided to these methods is not consistent with the target collection’s actual type, those methods will always
return false
or null
. This is most likely unintended and hides a design problem.
This rule raises an issue when the type of the argument of the following APIs is unrelated to the type used for the collection declaration:
-
Iterable<E>.contains
-
List<E>.remove
-
Map<K, V>.containsKey
-
Map<K, V>.containsValue
-
Map<K, V>.remove
-
Map<K, V>.[]
-
Queue<E>.remove
-
Set<E>.lookup
-
Set<E>.remove
Noncompliant code example
void foo(List<String> list, Map<Int, String> map) {
list.contains(100); // Noncompliant, list contains only Strings
list.remove(3.14); // Noncompliant
map.containsKey["a"]; // Noncompliant
map["123"]; // Noncompliant
}
Compliant solution
void foo(List<String> list, Map<Int, String> map) {
list.contains("100");
list.remove("3.14");
map.containsValue["a"];
map[123];
}