Skip to content

Files

Latest commit

 

History

History
63 lines (45 loc) · 1.14 KB

prefer_final_parameters.md

File metadata and controls

63 lines (45 loc) · 1.14 KB

Pattern: Missing use of final parameter

Issue: -

Description

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.

Example of incorrect code:

void badParameter(String label) { // LINT
 print(label);
}

Example of correct code:

void goodParameter(final String label) { // OK
 print(label);
}

Example of incorrect code:

void badExpression(int value) => print(value); // LINT

Example of correct code:

void goodExpression(final int value) => print(value); // OK

Example of incorrect code:

[1, 4, 6, 8].forEach((value) => print(value + 2)); // LINT

Example of correct code:

[1, 4, 6, 8].forEach((final value) => print(value + 2)); // OK

Example of correct code:

void mutableParameter(String label) { // OK
 print(label);
 label = 'Hello Linter!';
 print(label);
}

Further Reading