for_in_of_invalid_element_type
The type '{0}' used in the 'for' loop must implement '{1}' with a type argument that can be assigned to '{2}'.
Description
#The analyzer produces this diagnostic when the Iterable
or Stream
in a for-in loop has an element type that can't be assigned to the loop variable.
Example
#The following code produces this diagnostic because <String>[]
has an element type of String
, and String
can't be assigned to the type of e
(int
):
void f() {
for (int e in <String>[]) {
print(e);
}
}
Common fixes
#If the type of the loop variable is correct, then update the type of the iterable:
void f() {
for (int e in <int>[]) {
print(e);
}
}
If the type of the iterable is correct, then update the type of the loop variable:
void f() {
for (String e in <String>[]) {
print(e);
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.