avoid-nested-switch-expressions
Warns when a switch expression contains another switch expression.
Multiple nested switch expressions can significantly reduce readability.
⚙️ Config
Set acceptable-level
(default is 1
) to configure the acceptable nesting level.
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-nested-switch-expressions:
acceptable-level: 1
Example
❌ Bad:
(int x, int y) getPoint() => (1, 1);
void main() {
final value = switch (getPoint()) {
// LINT: Avoid nested switch expressions. Try rewriting the code to remove nesting.
(int y, int x) => switch (x) {
> 0 => 5,
== 0 => 6,
},
(int first, int second) => 2,
(int x, int y) => 3,
};
}
✅ Good:
(int x, int y) getPoint() => (1, 1);
void main() {
final value = switch (getPoint()) {
(int y, int x) => _validateX(x),
(int first, int second) => 2,
(int x, int y) => 3,
};
}
int _validateX(int x) => switch (x) {
> 0 => 5,
== 0 => 6,
};