dispose-provided-instances
Warns when an instance with a dispose
method created inside a provider does not have the dispose
method called in ref.onDispose
.
Not disposing such instances may lead to memory leaks and should be avoided.
Example
❌ Bad:
Provider.autoDispose((ref) {
final instance = DisposableService(); // LINT: This 'Provider' is not disposed, which can lead to a memory leak.
return instance;
});
class DisposableService {
void dispose() {}
}
✅ Good:
Provider.autoDispose((ref) {
final instance = DisposableService();
ref.onDispose(instance.dispose); // Correct, 'disposed' method is added to 'ref.onDispose'
return instance;
});
class DisposableService {
void dispose() {}
}