curly_braces_in_flow_control_structures
DO use curly braces for all flow control structures.
This rule is available as of Dart 2.0.
Rule sets: core, recommended, flutter
This rule has a quick fix available.
Details
#DO use curly braces for all flow control structures.
Doing so avoids the dangling else problem.
BAD:
if (overflowChars != other.overflowChars)
return overflowChars < other.overflowChars;
GOOD:
if (isWeekDay) {
print('Bike to work!');
} else {
print('Go dancing or read a book!');
}
There is one exception to this: an if
statement with no else
clause where
the entire if
statement (including the condition and the body) fits in one
line. In that case, you may leave off the braces if you prefer:
GOOD:
if (arg == null) return defaultValue;
If the body wraps to the next line, though, use braces:
GOOD:
if (overflowChars != other.overflowChars) {
return overflowChars < other.overflowChars;
}
Usage
#To enable the curly_braces_in_flow_control_structures
rule,
add curly_braces_in_flow_control_structures
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- curly_braces_in_flow_control_structures
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。