Skip to main content

prefer-type-over-var

added in: 1.7.0
🛠
Pro+

Warns when a variable is declared with the var keyword instead of a type.

Although var is shorter to write, the use of this keyword makes it difficult to understand the type of the declared variable.

Example

❌ Bad:

class SomeClass {
void method() {
// LINT: Prefer an explicit type annotation over 'var'.
var variable = nullableMethod();
}
}

// LINT: Prefer an explicit type annotation over 'var'.
var topLevelVariable = nullableMethod();

String? nullableMethod() => null;

✅ Good:

class SomeClass {
void method() {
String? variable = nullableMethod();
}
}

String? topLevelVariable = nullableMethod();

String? nullableMethod() => null;