Skip to main content

avoid-nested-switch-expressions

added in: 1.5.0
⚙️
Dart 3.0+

Warns when a switch expression contains another switch expression.

⚙️ Config

Set acceptable-level (default is `1) to configure the acceptable nesting level.

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()) {
(int y, int x) => switch (x) { // LINT
> 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,
};