avoid_function_literals_in_foreach_calls
Function literals shouldn't be passed to 'forEach'.
Description
#The analyzer produces this diagnostic when the argument to
Iterable.forEach
is a closure.
Example
#The following code produces this diagnostic because the argument to the
invocation of forEach
is a closure:
dart
void f(Iterable<String> s) {
s.forEach((e) => print(e));
}
Common fixes
#If the closure can be replaced by a tear-off, then replace the closure:
dart
void f(Iterable<String> s) {
s.forEach(print);
}
If the closure can't be replaced by a tear-off, then use a for
loop to
iterate over the elements:
dart
void f(Iterable<String> s) {
for (var e in s) {
print(e);
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。