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

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);
}

Configure avoid-late-keyword

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-late-keyword": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/avoid-late-keyword: <reason>