const_constructor_with_field_initialized_by_non_const
Can't define the 'const' constructor because the field '{0}' is initialized with a non-constant value.
Description
#The analyzer produces this diagnostic when a constructor has the keyword
const
, but a field in the class is initialized to a non-constant value.
Example
#The following code produces this diagnostic because the field s
is
initialized to a non-constant value:
dart
String x = '3';
class C {
final String s = x;
const C();
}
Common fixes
#If the field can be initialized to a constant value, then change the initializer to a constant expression:
dart
class C {
final String s = '3';
const C();
}
If the field can't be initialized to a constant value, then remove the
keyword const
from the constructor:
dart
String x = '3';
class C {
final String s = x;
C();
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。