prefer_final_parameters
Prefer final for parameter declarations if they are not reassigned.
This rule is available as of Dart 2.14.0.
This rule has a quick fix available.
Incompatible rules: unnecessary_final, avoid_final_parameters
Details
DO prefer declaring parameters as final if they are not reassigned in the function body.
Declaring parameters as final when possible is a good practice because it helps avoid accidental reassignments.
BAD:
void badParameter(String label) { // LINT
print(label);
}
GOOD:
void goodParameter(final String label) { // OK
print(label);
}
BAD:
void badExpression(int value) => print(value); // LINT
GOOD:
void goodExpression(final int value) => print(value); // OK
BAD:
[1, 4, 6, 8].forEach((value) => print(value + 2)); // LINT
GOOD:
[1, 4, 6, 8].forEach((final value) => print(value + 2)); // OK
GOOD:
void mutableParameter(String label) { // OK
print(label);
label = 'Hello Linter!';
print(label);
}
Usage
To enable the prefer_final_parameters
rule,
add prefer_final_parameters
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- prefer_final_parameters