prefer_final_locals

Group: style

Maturity: stable

Dart SDK: >= 2.0.0 • (Linter v0.1.27)

Since info is static, may be stale
has-fix

View all Lint Rules

Using the Linter

DO prefer declaring variables as final if they are not reassigned later in the code.

Declaring variables as final when possible is a good practice because it helps avoid accidental reassignments and allows the compiler to do optimizations.

BAD:

void badMethod() {
  var label = 'hola mundo! badMethod'; // LINT
  print(label);
}

GOOD:

void goodMethod() {
  final label = 'hola mundo! goodMethod';
  print(label);
}

GOOD:

void mutableCase() {
  var label = 'hola mundo! mutableCase';
  print(label);
  label = 'hello world';
  print(label);
}

Incompatible with: unnecessary_final.