no_ default_ cases
Learn about the no_default_cases linter rule.
No default cases.
Details
#
Switches on enums and enum-like classes should not use a
default clause.
Enum-like classes are defined as concrete (non-abstract) classes that have:
- only private non-factory constructors
- two or more static const fields whose type is the enclosing class and
- no subclasses of the class in the defining library
DO define default behavior outside switch statements.
BAD:
switch (testEnum) {
case TestEnum.A:
return '123';
case TestEnum.B:
return 'abc';
default:
return null;
}
GOOD:
switch (testEnum) {
case TestEnum.A:
return '123';
case TestEnum.B:
return 'abc';
}
// Default here.
return null;
Enable
#
To enable the no_default_cases rule, add
no_default_cases under
linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- no_default_cases
If you're instead using the YAML map syntax to configure
linter rules, add no_default_cases: true under
linter > rules:
linter:
rules:
no_default_cases: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.