1

I'm getting this lint when using forEach:

Avoid using `forEach` with a function literal.

This a sample code:

class AB {
  int data;
  AB(this.data);
  int call() => data;
}

void main() {
  var myList = [AB(1), AB(3)];
  myList.forEach((e) => print(e()));  // <--- lint raised here
}
  • 2
    Because except in trivial cases, [you should prefer using a regular `for` loop](https://stackoverflow.com/a/65420010/). – jamesdlin Feb 18 '22 at 16:42

1 Answers1

1

The lint is based on the style guide, which says that you should use a for/in loop instead.

for (var e in myList) {
  print(e());
}

That is both more efficient and more readable.

lrn
  • 64,680
  • 7
  • 105
  • 121