unnecessary-late
Same as unnecessary_late in Dart lints
- Group
- Style
- Path
lint/style/unnecessary-late- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 458 of 512
Flags a redundant late modifier on an initialized static or top-level variable.
Static fields and top-level variables in Dart are already initialized lazily:
their initializer does not run until the variable is first read. Marking such
a declaration late therefore buys nothing, and it misleads readers into
thinking deferred initialization is significant here. The rule only fires when
the declarator actually has an initializer, since late on an uninitialized
static or top-level variable does carry meaning. Drop the late keyword.
Invalid
// `late` on already-lazy static / top-level variables with initializers.
late String banner = 'hello';
late int total = compute();
// `late final` with an initializer is equally redundant: static / top-level
// finals are already lazily initialized, so `late` still adds nothing.
late final int cachedTotal = compute();
int compute() => 42;
class Service {
static late Service instance = Service._();
static late int counter = 0;
static late final int cached = 0;
Service._();
}
class Cache {
static late String key = 'k';
static late bool ready = true;
}// `late` on already-lazy static / top-level variables with initializers.late String banner = 'hello';late String banner = 'hello';late int total = compute();// finals are already lazily initialized, so `late` still adds nothing.late final int cachedTotal = compute();class Service { static late Service instance = Service._(); static late Service instance = Service._(); static late int counter = 0; static late int counter = 0; static late final int cached = 0;class Cache { static late String key = 'k'; static late String key = 'k'; static late bool ready = true;Valid
// `late` that is genuinely needed, or plain declarations.
late final String deferred;
String loadConfig() => 'cfg';
class Service {
late final Service instance;
static final ready = true;
static int counter = 0;
late int lazyInstanceField = 1;
Service();
}
final topLevel = loadConfig();
String plain = 'x';Configure unnecessary-late
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"unnecessary-late": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/unnecessary-late: <reason>More style rules
- unnecessary-library-directive
unnecessary_library_directiverecommendedFlags a
librarydirective that has neither a doc comment nor annotations. - unnecessary-library-name
unnecessary_library_namerecommendedFlags a
librarydirective that carries an explicit name. - unnecessary-new
unnecessary_newrecommendedFlags an explicit
newkeyword in an instance-creation expression. - unnecessary-null-aware-operator-on-extension-on-nullable
unnecessary_null_aware_operator_on_extension_on_nullableFlags a null-aware access whose member comes from an extension on a nullable type.
- unnecessary-null-checks
unnecessary_null_checksFlags a null assertion
!on a value that flows into a nullable context. - unnecessary-nullable-for-final-variable-declarations
unnecessary_nullable_for_final_variable_declarationsrecommendedFlags a
finalorconstvariable given a nullable type but initialized to a provably non-null value. - unnecessary-parenthesis
unnecessary_parenthesisrecommendedFlags parentheses that can be removed without changing the meaning or hurting readability.
- unnecessary-raw-strings
unnecessary_raw_stringsrecommendedFlags raw string literals that contain no backslash or
$.