目录

avoid_empty_else

Avoid empty statements in else clauses.

此规则自 Dart 2.0 版本起可用。

_规则集:core , recommended , flutter _

此规则提供 快速修复

详情

#

AVOID empty statements in the else clause of if statements.

BAD:

dart
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:

dart
if (x > y)
  print('1');
else
  print('2');

GOOD:

dart
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:

dart
if (x > y) print('1');

print('2');

使用方法

#

要启用 avoid_empty_else 规则,请在你的 analysis_options.yaml 文件中,在 linter > rules 下添加 avoid_empty_else

analysis_options.yaml
yaml
linter:
  rules:
    - avoid_empty_else