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:
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:
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:
String x = '3';
class C {
final String s = x;
C();
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-4. View source or report an issue.