var_ with_ no_ type_ annotation
Learn about the var_with_no_type_annotation linter rule.
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:
void func(var x) { // LINT
print(x);
}
GOOD:
void func(String x) { // OK
print(x);
}
BAD:
[1, 4, 6, 8].forEach((var value) => print(value + 2)); // LINT
GOOD:
[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:
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:
linter:
rules:
var_with_no_type_annotation: true
Unless stated otherwise, the documentation on this site reflects Dart 3.11.0. Report an issue.