case_expression_type_is_not_switch_expression_subtype
The switch case expression type '{0}' must be a subtype of the switch expression type '{1}'.
Description
#The analyzer produces this diagnostic when the expression following case
in a switch
statement has a static type that isn't a subtype of the
static type of the expression following switch
.
Example
#The following code produces this diagnostic because 1
is an int
, which
isn't a subtype of String
(the type of s
):
void f(String s) {
switch (s) {
case 1:
break;
}
}
Common fixes
#If the value of the case
expression is wrong, then change the case
expression so that it has the required type:
void f(String s) {
switch (s) {
case '1':
break;
}
}
If the value of the case
expression is correct, then change the switch
expression to have the required type:
void f(int s) {
switch (s) {
case 1:
break;
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。