always_ put_ control_ body_ on_ new_ line
Learn about the always_put_control_body_on_new_line linter rule.
Separate the control structure expression from its statement.
Details
#From the style guide for the flutter repo:
DO separate the control structure expression from its statement.
Don't put the statement part of an if,
for, while, do
on the same line as the expression, even if it is short. Doing
so makes it unclear that there is relevant code there. This is
especially important for early returns.
BAD:
if (notReady) return;
if (notReady)
return;
else print('ok')
while (condition) i += 1;
GOOD:
if (notReady)
return;
if (notReady)
return;
else
print('ok')
while (condition)
i += 1;
Note that this rule can conflict with the Dart formatter, and should not be enabled when the Dart formatter is used.
Enable
#
To enable the
always_put_control_body_on_new_line rule, add
always_put_control_body_on_new_line
under
linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- always_put_control_body_on_new_line
If you're instead using the YAML map syntax to configure
linter rules, add
always_put_control_body_on_new_line: true under
linter > rules:
linter:
rules:
always_put_control_body_on_new_line: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.