collection_methods_unrelated_type
The argument type '{0}' isn't related to '{1}'.
Description
#The analyzer produces this diagnostic when any one of several methods in the core libraries are invoked with arguments of an inappropriate type. These methods are ones that don't provide a specific enough type for the parameter to allow the normal type checking to catch the error.
The arguments that are checked are:
- an argument to
Iterable<E>.contains
should be related toE
- an argument to
List<E>.remove
should be related toE
- an argument to
Map<K, V>.containsKey
should be related toK
- an argument to
Map<K, V>.containsValue
should be related toV
- an argument to
Map<K, V>.remove
should be related toK
- an argument to
Map<K, V>.[]
should be related toK
- an argument to
Queue<E>.remove
should be related toE
- an argument to
Set<E>.lookup
should be related toE
- an argument to
Set<E>.remove
should be related toE
Example
#The following code produces this diagnostic because the argument to
contains
is a String
, which isn't assignable to int
, the element
type of the list l
:
bool f(List<int> l) => l.contains('1');
Common fixes
#If the element type is correct, then change the argument to have the same type:
bool f(List<int> l) => l.contains(1);
If the argument type is correct, then change the element type:
bool f(List<String> l) => l.contains('1');
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。