Group: errors
Maturity: stable
Dart SDK: >= 2.19.0 • (Linter v1.29.0)
Since info is static, may be staleDON'T invoke certain collection method with an argument with an unrelated type.
Doing this will invoke ==
on the collection's elements and most likely will
return false
.
An argument passed to a collection method should relate to the collection type as follows:
Iterable<E>.contains
should be related to E
List<E>.remove
should be related to E
Map<K, V>.containsKey
should be related to K
Map<K, V>.containsValue
should be related to V
Map<K, V>.remove
should be related to K
Map<K, V>.[]
should be related to K
Queue<E>.remove
should be related to E
Set<E>.lookup
should be related to E
Set<E>.remove
should be related to E
BAD:
void someFunction() {
var list = <int>[];
if (list.contains('1')) print('someFunction'); // LINT
}
BAD:
void someFunction() {
var set = <int>{};
set.remove('1'); // LINT
}
GOOD:
void someFunction() {
var list = <int>[];
if (list.contains(1)) print('someFunction'); // OK
}
GOOD:
void someFunction() {
var set = <int>{};
set.remove(1); // OK
}
To enable the collection_methods_unrelated_type
lint,
add collection_methods_unrelated_type
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- collection_methods_unrelated_type