const_ constructor_ with_ non_ final_ field
Details about the 'const_constructor_with_non_final_field' diagnostic produced by the Dart analyzer.
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:
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:
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:
class C {
int x;
C(this.x);
}
Unless stated otherwise, the documentation on this site reflects Dart 3.10.3. Report an issue.