Skip to main content

unnecessary_lambdas

Closure should be a tearoff.

Description

#

The analyzer produces this diagnostic when a closure (lambda) could be replaced by a tear-off.

Example

#

The following code produces this diagnostic because the closure passed to forEach contains only an invocation of the function print with the parameter of the closure:

dart
void f(List<String> strings) {
  strings.forEach((string) {
    print(string);
  });
}

Common fixes

#

Replace the closure with a tear-off of the function or method being invoked with the closure:

dart
void f(List<String> strings) {
  strings.forEach(print);
}