no_ default_ cases
Details about the 'no_default_cases' diagnostic produced by the Dart analyzer.
Invalid use of 'default' member in a switch.
Description
#
The analyzer produces this diagnostic when a switch statement over an
enum value or an instance of an enum-like class has a default clause.
Enum-like classes are non-abstract classes that have:
- Only private, non-factory constructors.
- Two or more
static constfields whose type is the enclosing class. - No subclasses in the defining library.
Examples
#
The following code produces this diagnostic because the
switch statement over the enum value e has a default
clause:
enum E { a, b, c }
void f(E e) {
switch (e) {
case .a:
print('a');
default:
print('other');
}
}
The following code produces this diagnostic because the
switch statement over c, an instance of the enum-like class
C,
has a default clause:
class const C._(final int i) {
static const C a = ._(1);
static const C b = ._(2);
static const C c = ._(3);
}
void f(C c) {
switch (c) {
case .a:
print('a');
default:
print('other');
}
}
Common fixes
#
If the value is an enum value,
replace the default clause with case clauses for
the enum values that aren't already handled:
enum E { a, b, c }
void f(E e) {
switch (e) {
case .a:
print('a');
case .b:
case .c:
print('other');
}
}
If the value is an instance of an enum-like class,
replace the default clause with case clauses for
the constants that aren't already handled:
class const C._(final int i) {
static const C a = ._(1);
static const C b = ._(2);
static const C c = ._(3);
}
void f(C c) {
switch (c) {
case .a:
print('a');
case .b:
case .c:
print('other');
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.12.2. Report an issue.