prefer_typing_uninitialized_variables
Prefer typing uninitialized variables and fields.
This rule is available as of Dart 2.0.
Rule sets: core, recommended, flutter
This rule has a quick fix available.
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:
dart
class BadClass {
static var bar; // LINT
var foo; // LINT
void method() {
var bar; // LINT
bar = 5;
print(bar);
}
}
BAD:
dart
void aFunction() {
var bar; // LINT
bar = 5;
...
}
GOOD:
dart
class GoodClass {
static var bar = 7;
var foo = 42;
int baz; // OK
void method() {
int baz;
var bar = 5;
...
}
}
Usage
#To enable the prefer_typing_uninitialized_variables
rule,
add prefer_typing_uninitialized_variables
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- prefer_typing_uninitialized_variables
除非另有说明,文档之所提及适用于 Dart 3.5.3 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。