unnecessary_final
Don’t use final
for local variables.
This rule is available as of Dart 2.7.0.
This rule has a quick fix available.
Incompatible rules: prefer_final_locals, prefer_final_parameters
Details
Use var
, not final
, when declaring local variables.
Per Effective Dart,
there are two styles in wide use. This rule enforces the var
style.
For the alternative style that prefers final
, enable prefer_final_locals
and prefer_final_in_for_each
instead.
For fields, final
is always recommended; see the rule prefer_final_fields
.
BAD:
void badMethod() {
final label = 'Final or var?';
for (final char in ['v', 'a', 'r']) {
print(char);
}
}
GOOD:
void goodMethod() {
var label = 'Final or var?';
for (var char in ['v', 'a', 'r']) {
print(char);
}
}
Usage
To enable the unnecessary_final
rule,
add unnecessary_final
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- unnecessary_final