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>.containsshould be related toE - an argument to
List<E>.removeshould be related toE - an argument to
Map<K, V>.containsKeyshould be related toK - an argument to
Map<K, V>.containsValueshould be related toV - an argument to
Map<K, V>.removeshould be related toK - an argument to
Map<K, V>.[]should be related toK - an argument to
Queue<E>.removeshould be related toE - an argument to
Set<E>.lookupshould be related toE - an argument to
Set<E>.removeshould 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');
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.