avoid_empty_else
Avoid empty statements in else clauses.
Details
#AVOID empty statements in the else
clause of if
statements.
BAD:
if (x > y)
print('1');
else ;
print('2');
If you want a statement that follows the empty clause to conditionally run,
remove the dangling semicolon to include it in the else
clause.
Optionally, also enclose the else's statement in a block.
GOOD:
if (x > y)
print('1');
else
print('2');
GOOD:
if (x > y) {
print('1');
} else {
print('2');
}
If you want a statement that follows the empty clause to unconditionally run,
remove the else
clause.
GOOD:
if (x > y) print('1');
print('2');
Enable
#To enable the avoid_empty_else
rule,
add avoid_empty_else
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- avoid_empty_else
If you're instead using the YAML map syntax to configure linter rules,
add avoid_empty_else: true
under linter > rules:
linter:
rules:
avoid_empty_else: true
除非另有说明,文档之所提及适用于 Dart 3.7.2 版本,本页面最后更新时间: 2025-03-07。 查看文档源码 或者 报告页面问题。