invalid_assignment
A value of type '{0}' can't be assigned to a variable of type '{1}'.
Description
#The analyzer produces this diagnostic when the static type of an expression that is assigned to a variable isn't assignable to the type of the variable.
Example
#The following code produces this diagnostic because the type of the
initializer (int
) isn't assignable to the type of the variable
(String
):
int i = 0;
String s = i;
Common fixes
#If the value being assigned is always assignable at runtime, even though the static types don't reflect that, then add an explicit cast.
Otherwise, change the value being assigned so that it has the expected type. In the previous example, this might look like:
int i = 0;
String s = i.toString();
If you can't change the value, then change the type of the variable to be compatible with the type of the value being assigned:
int i = 0;
int s = i;
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。