autoDispose<T> method
T
autoDispose<T>(
- T object
Registers an object to be automatically cleaned up when this controller is closed.
The object is returned to allow for inline chaining during initialization.
Supported Types
- LxReactive: Invokes
close(). - StreamSubscription: Invokes
cancel(). - Timer: Invokes
cancel(). - Sink: Invokes
close(). - Functions:
void Function()is called as a cleanup callback. - Duck Typing: Objects with
dispose(),close(), orcancel()methods.
// Example usage:
class MyController extends LevitController {
late final scrollController = autoDispose(ScrollController());
late final sub = autoDispose(myStream.listen((_) {}));
}
Returns the same object instance passed in.
Implementation
T autoDispose<T>(T object) {
// Check for identity to allow equal-but-distinct reactive variables
final alreadyAdded =
_disposables.any((element) => identical(element, object));
if (!alreadyAdded) {
_disposables.add(object);
}
// Auto-linking: If the reactive object doesn't have an ownerId, adopt it.
if (object is LxReactive) {
if (object.ownerId == null) {
object.ownerId = ownerPath;
}
}
return object;
}