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;
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。