disposeSingleton<T extends Object> static method
Disposes a specific singleton of type T
from the module.
This method allows you to dispose of a single dependency instead of the entire module. It will remove the dependency from the injector and call its dispose method if it's disposable.
Example:
Module.disposeSingleton<MyService>(context);
Implementation
static T? disposeSingleton<T extends Object>(BuildContext context) {
final closestModule = EasyModuleInheritedWidget.of(context, listen: false)?.module;
if (closestModule == null) {
throw Exception('No $EasyModule found in the widget tree');
}
if (closestModule.injector == null) {
throw Exception('$EasyModule ${closestModule.runtimeType} is not initialized');
}
try {
return closestModule.injector!.disposeSingleton<T>();
} catch (e) {
throw Exception('Failed to dispose type $T in module ${closestModule.runtimeType}: $e');
}
}