Cleanup class abstract
Bag of cleanup callbacks collected during onStart, executed in LIFO
order on feature deactivation.
Disposers may be sync or async (FutureOr<void> Function()). Async
disposers are awaited before the next one runs, so cleanup ordering
is preserved.
feature.onStart((api, cleanup) {
// The verbose form — works for any disposer:
cleanup.add(api.of(other).store.subscribe(listener));
final ws = WebSocket.connect(url);
cleanup.add(ws.close); // async disposer (Future<void>)
// For common patterns, prefer the typed helpers:
cleanup.subscribe(api.own.counter, (prev, next) => /* ... */);
cleanup.periodic(Duration(seconds: 30), () => api.own.data.refresh());
cleanup.listen(eventStream, (event) => /* ... */);
});
After deactivation the bag is sealed: subsequent add calls invoke
the disposer immediately, which makes async onStart that races with
deactivation safe by default. Late-added async disposers are
fire-and-forget — add itself stays sync.
Constructors
- Cleanup()
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
add(
FutureOr< void> disposer()) → void - Registers a disposer to run on deactivation (LIFO order).
-
listen<
T> (Stream< T> stream, void onData(T data), {Function? onError, void onDone()?, bool? cancelOnError}) → void -
Subscribes to
streamand registers the subscription'scanceldisposer on this bag. Every parameter mirrors Stream.listen. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
periodic(
Duration duration, void callback()) → void -
Schedules
callbackto fire everydurationuntil the bag runs. Wraps Timer.periodic and registerstimer.cancelon this bag — the timer is cancelled automatically on deactivation. -
subscribe<
T> (Store< T> store, StateChangeListener<T> listener, {bool fireImmediately = false}) → void -
Subscribes
listenertostoreand registers the cancel disposer on this bag. Sugar for the common patterncleanup.add(store.subscribe(listener, ...)). -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited