case_ expression_ type_ is_ not_ switch_ expression_ subtype
Details about the 'case_expression_type_is_not_switch_expression_subtype' diagnostic produced by the Dart analyzer.
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.12.2 版本报告页面问题.