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);
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.