prefer_typing_uninitialized_variables

Group: style

Maturity: stable

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

View the Project on GitHub dart-lang/linter

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;
    ...
  }
}