Skip to main content

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);
  }
}