Rules / Style / avoid-late-keyword
avoid-late-keyword
lint/style/avoid-late-keywordrecommended
Same as avoid-late-keyword · Dart Code MetricsFlags use of the late keyword on variables and fields.
late defers initialization and moves the "is it set?" check from compile
time to runtime: reading a late variable before it is assigned throws a
LateInitializationError rather than failing to compile. Preferring eagerly
initialized or nullable declarations keeps that guarantee static and the
failure mode visible. Top-level variables, instance fields, and local
declarations inside any function body are all checked.
Invalid
example.dartdart
// Test cases for avoid-late-keyword rule
// All lines with violations have the expect annotation at end of line.
class TestClass {
late int counter;
late String name;
late List<String> items;
late bool isActive;
}
void initializeWithLate() {
late final String message = "hello";
late var dynamicValue;
}
class Widget {
late BuildContext context;
void setup(BuildContext ctx) {
context = ctx;
}
}
mixin StateManagement {
late final controller = Controller();
}
late int topLevel = 0;
Avoid using the late keyword — use nullable types or initialize immediately instead
4class TestClass {
5 late int counter;
∙
Avoid using the late keyword — use nullable types or initialize immediately instead
5 late int counter;
6 late String name;
∙
Avoid using the late keyword — use nullable types or initialize immediately instead
6 late String name;
7 late List<String> items;
∙
Avoid using the late keyword — use nullable types or initialize immediately instead
7 late List<String> items;
8 late bool isActive;
∙
Avoid using the late keyword — use nullable types or initialize immediately instead
11void initializeWithLate() {
12 late final String message = "hello";
∙
Avoid using the late keyword — use nullable types or initialize immediately instead
12 late final String message = "hello";
13 late var dynamicValue;
∙
Avoid using the late keyword — use nullable types or initialize immediately instead
16class Widget {
17 late BuildContext context;
∙
Avoid using the late keyword — use nullable types or initialize immediately instead
24mixin StateManagement {
25 late final controller = Controller();
∙
Avoid using the late keyword — use nullable types or initialize immediately instead
27
28late int topLevel = 0;
∙
Valid
example.dartdart
// Good cases for avoid-late-keyword rule
// No violations expected
class TestClass {
int? counter;
String name = '';
List<String> items = [];
bool isActive = false;
}
void initializeWithDefaults() {
final String message = "hello";
final dynamicValue = null;
}
class Widget {
BuildContext? context;
void setup(BuildContext ctx) {
context = ctx;
}
}
mixin StateManagement {
final controller = Controller();
}
int topLevel = 0;
class DelayedInit {
String? _delayedValue;
void initialize() {
_delayedValue = "initialized";
}
String getValue() => _delayedValue ?? '';
}
class OptionalField {
final int? maybeValue;
OptionalField(this.maybeValue);
}
How to configure
Set the severity of avoid-late-keyword in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"avoid-late-keyword": "error"
}
}
}
}