curly_braces_in_flow_control_structures
DO use curly braces for all flow control structures.
此规则自 Dart 2.0 版本起可用。
_规则集:core , recommended , flutter _
此规则提供 快速修复 。
详情
#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;
}
使用方法
#要启用 curly_braces_in_flow_control_structures
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 curly_braces_in_flow_control_structures
:
linter:
rules:
- curly_braces_in_flow_control_structures
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.