prefer_ typing_ uninitialized_ variables
Learn about the prefer_typing_uninitialized_variables linter rule.
Prefer typing uninitialized variables and fields.
Details
#PREFER specifying a type annotation for uninitialized variables and fields.
Forgoing type annotations for uninitialized variables is a bad practice because you may accidentally assign them to a type that you didn't originally intend to.
BAD:
class BadClass { static var bar; // LINT var foo; // LINT void method() { var bar; // LINT bar = 5; print(bar); } } BAD:
void aFunction() { var bar; // LINT bar = 5; ... } GOOD:
class GoodClass { static var bar = 7; var foo = 42; int baz; // OK void method() { int baz; var bar = 5; ... } }
Enable
# To enable the prefer_typing_uninitialized_variables rule, add prefer_typing_uninitialized_variables under linter > rules in your analysis_options.yaml file:
linter: rules: - prefer_typing_uninitialized_variables If you're instead using the YAML map syntax to configure linter rules, add prefer_typing_uninitialized_variables: true under linter > rules:
linter: rules: prefer_typing_uninitialized_variables: true Unless stated otherwise, the documentation on this site reflects Dart 3.10.3. Report an issue.