for_in_with_const_variable
A for-in loop variable can't be a 'const'.
Description
#The analyzer produces this diagnostic when the loop variable declared in a
for-in loop is declared to be a const
. The variable can't be a const
because the value can't be computed at compile time.
Example
#The following code produces this diagnostic because the loop variable x
is declared to be a const
:
dart
void f() {
for (const x in [0, 1, 2]) {
print(x);
}
}
Common fixes
#If there's a type annotation, then remove the const
modifier from the
declaration.
If there's no type, then replace the const
modifier with final
, var
,
or a type annotation:
dart
void f() {
for (final x in [0, 1, 2]) {
print(x);
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。