Dart Analyze logoDart Analyze/
DRT-W1270

Const variables must be initialized with a constant valueDRT-W1270

Major severityMajor
Bug Risk categoryBug Risk

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:

var x = 0;
const y = x;

Common fixes

If the value being assigned can be declared to be const, then change the declaration:

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:

var x = 0;
final y = x;