field_initializer_not_assignable
The initializer type '{0}' can't be assigned to the field type '{1}' in a const constructor.
The initializer type '{0}' can't be assigned to the field type '{1}'.
Description
#The analyzer produces this diagnostic when the initializer list of a constructor initializes a field to a value that isn't assignable to the field.
Example
#The following code produces this diagnostic because 0
has the type int
, and an int
can't be assigned to a field of type String
:
class C {
String s;
C() : s = 0;
}
Common fixes
#If the type of the field is correct, then change the value assigned to it so that the value has a valid type:
class C {
String s;
C() : s = '0';
}
If the type of the value is correct, then change the type of the field to allow the assignment:
class C {
int s;
C() : s = 0;
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.