Skip to main content

move-variable-outside-iteration

has auto-fix
pro+

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.

Example

❌ Bad:

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

for (final element in list) {
// LINT: This variable does not depend on the loop variables.
// Try moving the variable out.
final reversed = list.reversed;

...
}
}

✅ Good:

void fn() {
final list = [1, 2, 3];
final reversed = list.reversed; // Correct, calculated only once

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