Skip to content

Files

Latest commit

 

History

History
35 lines (27 loc) · 742 Bytes

prefer_final_in_for_each.md

File metadata and controls

35 lines (27 loc) · 742 Bytes

Pattern: Missing use of final in for-each loop variable

Issue: -

Description

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');
}

Further Reading