dispose-provider-resources
- Group
- Correctness
- Path
lint/correctness/dispose-provider-resources- Default
- On (recommended)
- Origin
- Falcon
- Domain
- riverpod
- Rule
- 224 of 512
Flags provider-created resources without autoDispose or ref.onDispose cleanup.
Resources created in a long-lived Riverpod provider need an explicit lifetime.
Use an autoDispose provider or register the resource's cleanup with ref.onDispose.
Checks package-confirmed Provider, FutureProvider and StreamProvider callbacks
for resolved constructor-created instances with a zero-required-argument
close or cancel method. Dispose types are left to dispose-provided-instances.
Borrowed values, deferred closures and unknown types are excluded. This
lifetime policy is separate from disposing arbitrary provided instances:
autoDispose alone does not invoke an object's dispose.
SDK sinks, sockets and subscriptions are left to the shipped leak rules unless
returned as provider state. Simple source-visible factories returning borrowed
values are excluded, while allocating factories remain eligible.
Invalid
import 'package:riverpod/riverpod.dart';
class Channel { Channel(); Channel.named(); void close() {} }
class Subscription { Subscription(); void cancel() {} }
class Service { Service(); void dispose() {} }
class Child extends Channel {}
final channel = Provider((ref) {
final resource = Channel();
return resource;
});
final subscription = Provider((ref) {
final resource = Subscription();
return resource;
});
final named = Provider((ref) {
final resource = Channel.named();
return resource;
});
final explicitNew = Provider((ref) {
final resource = new Channel();
return resource;
});
final inherited = Provider((ref) {
final resource = Child();
return resource;
});
final emptyCleanup = Provider((ref) {
final resource = Channel();
ref.onDispose(() {});
return resource;
});
final wrongCleanup = Provider((ref) {
final first = Channel();
final second = Channel();
ref.onDispose(second.close);
return first;
});final channel = Provider((ref) { final resource = Channel();final subscription = Provider((ref) { final resource = Subscription();final named = Provider((ref) { final resource = Channel.named();final explicitNew = Provider((ref) { final resource = new Channel();final inherited = Provider((ref) { final resource = Child();final emptyCleanup = Provider((ref) { final resource = Channel();final wrongCleanup = Provider((ref) { final first = Channel();Valid
import 'package:riverpod/riverpod.dart';
class Channel { Channel(); void close() {} }
class Subscription { Subscription(); void cancel() {} }
class Service { Service(); void dispose() {} }
class Value { Value(); }
final shared = Channel();
final automatic = Provider.autoDispose((ref) => Channel());
final automaticSubscription = Provider.autoDispose((ref) => Subscription());
final registered = Provider((ref) {
final resource = Channel();
ref.onDispose(resource.close);
return resource;
});
final blockCleanup = Provider((ref) {
final resource = Subscription();
ref.onDispose(() { resource.cancel(); });
return resource;
});
final disposed = Provider((ref) {
final resource = Service();
ref.onDispose(() => resource.dispose());
return resource;
});
final borrowed = Provider((ref) => shared);
final value = Provider((ref) => Value());
final deferred = Provider((ref) => () => Channel());
Channel outsideProvider() => Channel();
void ordinaryCallback(Object Function(Ref) callback) {}
void unrelatedApi() { ordinaryCallback((ref) => Channel()); }Configure dispose-provider-resources
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"dispose-provider-resources": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/dispose-provider-resources: <reason>More correctness rules
- dispose-providersrecommendedprovider
Requires cleanup callbacks for disposable values created by package:provider's Provider.
- emit-new-bloc-state-instancesrecommendedbloc
Flags Bloc and Cubit emissions that reuse their current state instance.
- exhaustive-cases
exhaustive_casesrecommendedRequire switch statements over enum-like classes to cover every constant value.
- function-always-returns-null
Flags value functions with a proven explicit null return on every path.
- function-always-returns-same-value
Warns when distinct return paths in a function produce the same scalar constant.
- handle-bloc-event-subclassesbloc
Warns when a Bloc constructor leaves a concrete event subclass without a handler.
- hash-and-equals
hash_and_equalsrecommendedRequire
hashCodeand==to be overridden together. - implementation-imports
implementation_importsrecommendedDisallow importing another package's private
src/libraries.