const_initialized_with_non_constant_value
Const variables must be initialized with a constant value.
Description
#The analyzer produces this diagnostic when a value that isn't statically
known to be a constant is assigned to a variable that's declared to be a
const
variable.
Example
#The following code produces this diagnostic because x
isn't declared to
be const
:
dart
var x = 0;
const y = x;
Common fixes
#If the value being assigned can be declared to be const
, then change the
declaration:
dart
const x = 0;
const y = x;
If the value can't be declared to be const
, then remove the const
modifier from the variable, possibly using final
in its place:
dart
var x = 0;
final y = x;
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。