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;
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.