assignment_to_const
Constant variables can't be assigned a value after initialization.
Description
#The analyzer produces this diagnostic when it finds an assignment to a
top-level variable, a static field, or a local variable that has the
const
modifier. The value of a compile-time constant can't be changed at
runtime.
Example
#The following code produces this diagnostic because c
is being assigned a
value even though it has the const
modifier:
const c = 0;
void f() {
c = 1;
print(c);
}
Common fixes
#If the variable must be assignable, then remove the const
modifier:
var c = 0;
void f() {
c = 1;
print(c);
}
If the constant shouldn't be changed, then either remove the assignment or use a local variable in place of references to the constant:
const c = 0;
void f() {
var v = 1;
print(v);
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。