prefer-type-over-var

Group
Style
Path
lint/style/prefer-type-over-var
Default
Off, opt in
Origin
Falcon
Rule
413 of 512

Prefer explicit types over var in ordinary variable declarations.

Checks local, field and top-level var declarations, regardless of initializer. Explicit types, final and const declarations are not checked. Destructuring patterns and implicit for-in binding forms are excluded.

Invalid

example.dartdart
int readCount() => 3;
var top = readCount();
class Box {
  var count = readCount();
}
void main() {
  var count = readCount();
  var first = readCount(), second = readCount();
  var number = 3;
  var values = <int>[];
  var unset;
}
This variable uses `var`; replace it with an explicit type annotation.
1int readCount() => 3;
2var top = readCount();
This variable uses `var`; replace it with an explicit type annotation.
3class Box {
4 var count = readCount();
This variable uses `var`; replace it with an explicit type annotation.
6void main() {
7 var count = readCount();
This variable uses `var`; replace it with an explicit type annotation.
7 var count = readCount();
8 var first = readCount(), second = readCount();
This variable uses `var`; replace it with an explicit type annotation.
7 var count = readCount();
8 var first = readCount(), second = readCount();
This variable uses `var`; replace it with an explicit type annotation.
8 var first = readCount(), second = readCount();
9 var number = 3;
This variable uses `var`; replace it with an explicit type annotation.
9 var number = 3;
10 var values = <int>[];
This variable uses `var`; replace it with an explicit type annotation.
10 var values = <int>[];
11 var unset;

Valid

example.dartdart
int readCount() => 3;
const topConstant = 3;
final topFinal = readCount();
class Box {
  Box();
  Box.named();
  final field = 3;
  static const constant = 3;
  int typedField = 3;
}
void main() {
  int number = 3;
  String text = 'hello';
  List<int> items = <int>[];
  Box box = Box();
  Box other = Box.named();
  Box explicitNew = new Box();
  dynamic unset;
  final finalValue = readCount();
  const constantValue = 3;
  int typed = readCount();
}

Configure prefer-type-over-var

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-type-over-var": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/prefer-type-over-var: <reason>