switch_expression_not_assignable
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
):
dart
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:
dart
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:
dart
void f(String s) {
switch (s) {
case '0':
break;
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。