const_constructor_with_non_final_field
Can't define a const constructor for a class with non-final fields.
Description
#The analyzer produces this diagnostic when a constructor is marked as a const constructor, but the constructor is defined in a class that has at least one non-final instance field (either directly or by inheritance).
Example
#The following code produces this diagnostic because the field x
isn't
final:
dart
class C {
int x;
const C(this.x);
}
Common fixes
#If it's possible to mark all of the fields as final, then do so:
dart
class C {
final int x;
const C(this.x);
}
If it isn't possible to mark all of the fields as final, then remove the
keyword const
from the constructor:
dart
class C {
int x;
C(this.x);
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。