Skip to main content

move-variable-outside-iteration

added in: 1.13.0
🛠
Pro+

Warns when a variable does not depend on the outer loop and can be moved out.

❌ Bad:

void fn() {
final list = [1, 2, 3];

for (final element in list) {
final reversed = list.reversed; // LINT

...
}
}

✅ Good:

void fn() {
final list = [1, 2, 3];
final reversed = list.reversed;

for (final element in list) {
...
}
}