move-variable-outside-iteration
Warns when a variable does not depend on the outer loop and can be moved out.
This rule helps avoid unnecessary declarations of variables that can be declared only once.
❌ Bad:
void fn() {
final list = [1, 2, 3];
for (final element in list) {
final reversed = list.reversed; // LINT: This variable does not depend on the loop variables. Try moving the variable out.
...
}
}
✅ Good:
void fn() {
final list = [1, 2, 3];
final reversed = list.reversed; // Correct, calculated only once
for (final element in list) {
...
}
}