avoid-nested-conditional-expressions
Suggests to refactor conditional expressions when the nesting level exceeds the configured threshold.
⚙️ Config
Set acceptable-level
(default is 1
) to configure the acceptable nesting level.
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-nested-conditional-expressions:
acceptable-level: 1
Example
❌ Bad:
final str = '';
final twoLevels = str.isEmpty
? str.isEmpty // LINT: Avoid nested conditional expressions. Try rewriting the code to remove nesting.
? 'hi'
: '1'
: '2';
final threeLevels = str.isEmpty
? str.isEmpty // LINT: Avoid nested conditional expressions. Try rewriting the code to remove nesting.
? str.isEmpty // LINT: Avoid nested conditional expressions. Try rewriting the code to remove nesting.
? 'hi'
: '1'
: '2'
: '3';
✅ Good:
final oneLevel = str.isEmpty ? 'hi' : '1'; // Correct, only one level (as configured)
final twoLevels = _getStr(str);
String _getStr(String str) {
if (str.isEmpty) {
return 'hi';
}
...
}