Rules / Style / prefer-typing-uninitialized-variables
prefer-typing-uninitialized-variables
Flags uninitialized variables declared without a type annotation.
When a declaration has no initializer, there is no value for the analyzer to
infer a type from, so var x; leaves the variable as dynamic — silently
opting out of static checking on every later use. Writing int x; instead
documents intent and restores type safety at no cost. The rule fires only on
declarators that are both untyped and uninitialized; a declaration is exempt
as soon as it carries a type annotation. It covers local variables, fields,
and top-level variables.
Invalid
example.dartdart
// Uninitialized variables without a type annotation.
var topLevel;
void f() {
var a;
var b;
late var d;
a = 1;
b = 2;
d = 3;
print(a + b + d);
}
class C {
var field;
static var s;
}
Prefer typing uninitialized variables and fields.
1// Uninitialized variables without a type annotation.
2var topLevel;
∙
Prefer typing uninitialized variables and fields.
Prefer typing uninitialized variables and fields.
Prefer typing uninitialized variables and fields.
Prefer typing uninitialized variables and fields.
14class C {
15 var field;
∙
Prefer typing uninitialized variables and fields.
15 var field;
16 static var s;
∙
Valid
example.dartdart
// Either a type annotation or an initializer is present.
int topLevel = 0;
void f() {
int a;
var b = 1;
final c = 2;
String? d;
double e = 0.0;
a = 5;
d = 'x';
print(b + c + e + a + d.length);
}
class C {
int field = 0;
var typed = 'x';
static int s = 1;
}
How to configure
Set the severity of prefer-typing-uninitialized-variables in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"prefer-typing-uninitialized-variables": "error"
}
}
}
}