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