Contents

unnecessary_final

Contents

Don't use final for local variables.

This rule is available as of Dart 2.7.

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:

dart
void badMethod() {
  final label = 'Final or var?';
  for (final char in ['v', 'a', 'r']) {
    print(char);
  }
}

GOOD:

dart
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:

analysis_options.yaml
yaml
linter:
  rules:
    - unnecessary_final