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);
}
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.