switch_ expression_ not_ assignable
Details about the 'switch_expression_not_assignable' diagnostic produced by the Dart analyzer.
Type '{0}' of the switch expression isn't assignable to the type '{1}' of case expressions.
Description
#
The analyzer produces this diagnostic when the type of the
expression in a
switch statement isn't assignable to the type of
the expressions in the case clauses.
Example
#
The following code produces this diagnostic because the type
of s (String) isn't assignable to
the type of 0 (int):
void f(String s) {
switch (s) {
case 0:
break;
}
}
Common fixes
#
If the type of the case expressions is correct,
then change the expression in the
switch statement to have the correct type:
void f(String s) {
switch (int.parse(s)) {
case 0:
break;
}
}
If the type of the switch expression is correct,
then change the case
expressions to have the correct type:
void f(String s) {
switch (s) {
case '0':
break;
}
}
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.