non_ exhaustive_ switch_ expression
Details about the 'non_exhaustive_switch_expression' diagnostic produced by the Dart analyzer.
The enum '{0}' isn't exhaustively matched by the switch cases because some of the enum constants are private.
The type '{0}' isn't exhaustively matched by the switch cases since it doesn't match the pattern '{1}'.
Description
#
The analyzer produces this diagnostic when a
switch expression 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
expression doesn't have a case for the value
E.three:
enum E { one, two, three }
String f(E e) => switch (e) {
E.one => 'one',
E.two => 'two',
};
Common fixes
#If the missing values are distinctly meaningful to the switch expression, then add a case for each of the values missing a match:
enum E { one, two, three }
String f(E e) => switch (e) {
E.one => 'one',
E.two => 'two',
E.three => 'three',
};
If the missing values don't need to be matched, then add a wildcard pattern that returns a simple default:
enum E { one, two, three }
String f(E e) => switch (e) {
E.one => 'one',
E.two => 'two',
_ => 'unknown',
};
Be aware that a wildcard pattern will handle any values added
to the type in the future. You will lose the ability to have
the compiler warn you if the switch needs to be
updated to account for newly added types.
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.