avoid-disposing-late-fields
- Group
- Correctness
- Path
lint/correctness/avoid-disposing-late-fields- Default
- Off, opt in
- Origin
- Falcon
- Domain
- flutter
- Rule
- 42 of 512
Avoid releasing late State fields that may be unset or initialized only by cleanup.
A direct = assignment, or a direct read of a lazily initialized field, in
synchronous initState is accepted in its initial straight-line prefix of
expression statements, local declarations and empty statements. Any other
statement ends that prefix, including assertions and pattern declarations.
Arrow bodies are supported. Reads cover bare fields, property receivers,
method receivers and cascade receivers, not nested arguments or callbacks.
Conditional, nested, asynchronous and helper-based initialization is not proven;
lazy field initializers alone still do not make cleanup safe.
Invalid
import 'package:flutter/material.dart' as f;
class Resource {
void dispose() {}
void close() {}
void cancel() {}
}
class S extends f.State<f.StatefulWidget> {
late Resource unset;
late final Resource lazy = Resource();
late Resource stream, subscription;
late Resource? optional;
late Resource first = Resource(), second = Resource();
void dispose() {
unset.dispose();
this.lazy.dispose();
stream.close();
subscription.cancel();
optional?.dispose();
optional!.close();
unset..dispose();
(this.lazy)..close();
first.dispose();
second.cancel();
super.dispose();
}
} void dispose() { unset.dispose(); unset.dispose(); this.lazy.dispose(); this.lazy.dispose(); stream.close(); stream.close(); subscription.cancel(); subscription.cancel(); optional?.dispose(); optional?.dispose(); optional!.close(); optional!.close(); unset..dispose(); unset..dispose(); (this.lazy)..close(); (this.lazy)..close(); first.dispose(); first.dispose(); second.cancel();Valid
import 'package:flutter/material.dart';
class Resource {
void dispose() {}
void close() {}
void cancel() {}
}
class S extends State<StatefulWidget> {
Resource? optional;
final eager = Resource();
late Resource deferred;
static late Resource shared;
void cleanup() { deferred.dispose(); }
void dispose() {
optional?.dispose();
eager.dispose();
shared.dispose();
final deferred = Resource();
deferred.dispose();
void release(Resource deferred) { deferred.dispose(); }
super.dispose();
}
}
class Other {
late Resource resource;
void dispose() { resource.dispose(); }
}
class Holder extends State<StatefulWidget> {
late Resource resource;
void dispose() {
final other = Other();
other.resource.dispose();
super.dispose();
}
}Configure avoid-disposing-late-fields
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-disposing-late-fields": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-disposing-late-fields: <reason>More correctness rules
- avoid-duplicate-bloc-event-handlersrecommendedbloc
Flags later registrations of the same event type in a Bloc constructor.
- avoid-duplicate-cascades
Flags repeated field or index assignment targets within one cascade.
- avoid-duplicate-collection-elements
Flags repeated scalar literals and stable local reads in list and set literals.
- avoid-duplicate-exports
Flags export directives whose visible names are already covered by another export.
- avoid-duplicate-factories
Flags factories in one type with the same implementation, ignoring factory names and alpha-renaming parameters by position.
- avoid-duplicate-initializers
Flags final/const variables with identical initializers in the same scope.
- avoid-duplicate-map-keysrecommended
Flags repeated literal keys in map literals, including literal spreads and collection
ifentries. - avoid-duplicate-mixins
Flags repeated mixins in a with clause or a same-library superclass chain.