Skip to main content

var_with_no_type_annotation

Learn about the var_with_no_type_annotation linter rule.

Unreleased
Fix available
Released in Dart 3.12

Avoid declaring parameters with var and no type annotation.

Details

#

AVOID declaring parameters with var and no type annotation.

Declaring formal parameters using var and no type annotation will be a compile-time error when primary constructors are enabled. This lint flags those parameters to ease migration.

BAD:

dart
void func(var x) { // LINT
  print(x);
}

GOOD:

dart
void func(String x) { // OK
  print(x);
}

BAD:

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

GOOD:

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

Enable

#

To enable the var_with_no_type_annotation rule, add var_with_no_type_annotation under linter > rules in your analysis_options.yaml file:

analysis_options.yaml
yaml
linter:
  rules:
    - var_with_no_type_annotation

If you're instead using the YAML map syntax to configure linter rules, add var_with_no_type_annotation: true under linter > rules:

analysis_options.yaml
yaml
linter:
  rules:
    var_with_no_type_annotation: true