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
):
dart
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:
dart
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:
dart
void f() {
for (String e in <String>[]) {
print(e);
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。