const_instance_field
Only static fields can be declared as const.
Description
#The analyzer produces this diagnostic when an instance field is marked as being const.
Example
#The following code produces this diagnostic because f
is an instance
field:
dart
class C {
const int f = 3;
}
Common fixes
#If the field needs to be an instance field, then remove the keyword
const
, or replace it with final
:
dart
class C {
final int f = 3;
}
If the field really should be a const field, then make it a static field:
dart
class C {
static const int f = 3;
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。