Skip to main content

avoid-nested-conditional-expressions

added in: 1.6.0
⚙️

Checks for nested conditional expressions.

⚙️ Config example

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

dart_code_metrics:
...
rules:
...
- avoid-nested-conditional-expressions:
acceptable-level: 2

Example

❌ Bad:

final str = '';

final oneLevel = str.isEmpty ? 'hi' : '1';

final twoLevels = str.isEmpty
? str.isEmpty // LINT
? 'hi'
: '1'
: '2';

final threeLevels = str.isEmpty
? str.isEmpty // LINT
? str.isEmpty // LINT
? 'hi'
: '1'
: '2'
: '3';

✅ Good:

final str = '';

final oneLevel = str.isEmpty ? 'hi' : '1';

final twoLevels = _getStr(str);

String _getStr(String str) {
if (str.isEmpty) {
return 'hi';
}

...
}