non_exhaustive_switch_statement
The type '{0}' is not exhaustively matched by the switch cases since it doesn't match '{1}'.
Description
#The analyzer produces this diagnostic when a switch
statement switching
over an exhaustive type is missing a case for one or more of the possible
values that could flow through it.
Example
#The following code produces this diagnostic because the switch statement
doesn't have a case for the value E.three
, and E
is an exhaustive
type:
dart
enum E { one, two, three }
void f(E e) {
switch (e) {
case E.one:
case E.two:
}
}
Common fixes
#Add a case for each of the constants that aren't currently being matched:
dart
enum E { one, two, three }
void f(E e) {
switch (e) {
case E.one:
case E.two:
break;
case E.three:
}
}
If the missing values don't need to be matched, then add a default
clause or a wildcard pattern:
dart
enum E { one, two, three }
void f(E e) {
switch (e) {
case E.one:
case E.two:
break;
default:
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。