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
):
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;
}
}
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.