Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Latest commit

 

History

History
63 lines (38 loc) · 1.13 KB

avoid-late-keyword.md

File metadata and controls

63 lines (38 loc) · 1.13 KB

Avoid late keyword

Rule id

avoid-late-keyword

Description

Warns when a field or variable is declared with a late keyword.

late keyword enforces a variable's constraints at runtime instead of at compile time and since the variable is not definitely initialized, every time it is read, a runtime check is inserted to make sure it has been assigned a value. If it hasn’t, an exception will be thrown.

Use this rule if you want to avoid unexpected runtime exceptions.

Example

Bad:

class Test {
  late final field = 'string'; // LINT

  final String anotherField = '';

  String? nullableField;

  late String uninitializedField; // LINT

  void method() {
    late final variable = 'string'; // LINT

    final anotherVariable = '';

    String? nullableVariable;

    late String uninitializedVariable; // LINT
  }
}

Good:

class Test {
  final field = 'string';

  final String anotherField = '';

  String? nullableField;

  String uninitializedField;

  void method() {
    final variable = 'string';

    final anotherVariable = '';

    String? nullableVariable;

    String uninitializedVariable;
  }
}