avoid_ function_ literals_ in_ foreach_ calls
Learn about the avoid_function_literals_in_foreach_calls linter rule.
Avoid using forEach with a function literal.
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 awaits, while
the closure body of a forEach cannot.
BAD:
people.forEach((person) {
...
});
GOOD:
for (var person in people) {
...
}
Enable
#
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
If you're instead using the YAML map syntax to configure
linter rules, add
avoid_function_literals_in_foreach_calls: true
under linter > rules:
linter:
rules:
avoid_function_literals_in_foreach_calls: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.