variable_type_mismatch
A value of type '{0}' can't be assigned to a const variable of type '{1}'.
Description
#The analyzer produces this diagnostic when the evaluation of a constant
expression would result in a CastException
.
Example
#The following code produces this diagnostic because the value of x
is an
int
, which can't be assigned to y
because an int
isn't a String
:
dart
const dynamic x = 0;
const String y = x;
Common fixes
#If the declaration of the constant is correct, then change the value being assigned to be of the correct type:
dart
const dynamic x = 0;
const String y = '$x';
If the assigned value is correct, then change the declaration to have the correct type:
dart
const int x = 0;
const int y = x;
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。