avoid_function_literals_in_foreach_calls
Avoid using forEach
with a function literal.
This rule is available as of Dart 2.0.
Rule sets: recommended, flutter
This rule has a quick fix available.
Details
#AVOID using forEach
with a function literal.
The for
loop enables a developer to be clear and explicit as to their intent.
A return in the body of the for
loop returns from the body of the function,
where as a return in the body of the forEach
closure only returns a value
for that iteration of the forEach
. The body of a for
loop can contain
await
s, while the closure body of a forEach
cannot.
BAD:
people.forEach((person) {
...
});
GOOD:
for (var person in people) {
...
}
Usage
#To enable the avoid_function_literals_in_foreach_calls
rule,
add avoid_function_literals_in_foreach_calls
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- avoid_function_literals_in_foreach_calls
除非另有说明,文档之所提及适用于 Dart 3.5.3 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。