Pattern: Missing use of final
in for-each loop variable
Issue: -
Declaring for-each loop variables as final
when possible is a good practice
because it helps avoid accidental reassignments and allows the compiler to do
optimizations.
Example of incorrect code:
for (var element in elements) { // LINT
print('Element: $element');
}
Example of correct code:
for (final element in elements) {
print('Element: $element');
}
Example of correct code:
for (var element in elements) {
element = element + element;
print('Element: $element');
}