avoid-late-keyword
Same as avoid-late-keyword in Dart Code Metrics
- Group
- Style
- Path
lint/style/avoid-late-keyword- Default
- Off, opt in
- Origin
- Dart Code Metrics
- Rule
- 82 of 512
Flags 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
// 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;class TestClass { late int counter; late int counter; late String name; late String name; late List<String> items; late List<String> items; late bool isActive;void initializeWithLate() { late final String message = "hello"; late final String message = "hello"; late var dynamicValue;class Widget { late BuildContext context;mixin StateManagement { late final controller = Controller(); late int topLevel = 0;Valid
// 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);
}Configure avoid-late-keyword
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"avoid-late-keyword": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-late-keyword: <reason>More style rules
- avoid-multiple-declarations-per-line
avoid_multiple_declarations_per_linerecommendedFlags a variable declaration that declares more than one variable.
- avoid-non-null-assertion
avoid-non-null-assertionFlags the null-assertion operator
!except on a resolvedMapindex. - avoid-null-checks-in-equality-operators
avoid_null_checks_in_equality_operatorsrecommendedFlags null checks of the parameter inside a custom
operator ==. - avoid-positional-boolean-parameters
avoid_positional_boolean_parametersFlags a positional
boolparameter in a public signature. - avoid-positional-fields-in-records
avoid_positional_fields_in_recordsFlags positional fields in record types.
- avoid-private-typedef-functions
avoid_private_typedef_functionsFlags a private function typedef that is used at most once.
- avoid-public-notifier-propertiesriverpod
Keeps notifier state behind Riverpod's observable state property.
- avoid-redundant-argument-values
avoid_redundant_argument_valuesFlags arguments that pass exactly the parameter's default value.