RaiiListenableListener<T extends Listenable> class
A lifecycle implementation that manages the lifecycle of a listener attached to a Listenable.
This class provides automatic management of listeners for any Listenable object (such as ChangeNotifier, ValueNotifier, or custom implementations). It ensures that listeners are properly added during initialization and removed during disposal.
Example:
// Basic usage with ValueNotifier
final counter = ValueNotifier(0).withLifecycle(lifecycleAware);
RaiiListenableListener.withLifecycle(
lifecycleAware,
listenable: counter,
onListen: () => print('Counter changed: ${counter.value}'),
debugLabel: 'CounterListener',
);
// Usage with animation controller
final animation = AnimationController(vsync: this)
.withLifecycle(lifecycleAware);
RaiiListenableListener.withLifecycle(
lifecycleAware,
listenable: animation,
onListen: () => print('Animation value: ${animation.value}'),
debugLabel: 'AnimationListener',
);
// Usage with custom ChangeNotifier
class UserModel extends ChangeNotifier {
String name = '';
void updateName(String newName) {
name = newName;
notifyListeners();
}
}
final user = UserModel();
RaiiListenableListener.withLifecycle(
lifecycleAware,
listenable: user,
onListen: () => print('User updated: ${user.name}'),
debugLabel: 'UserModelListener',
);
- Mixed-in types
Constructors
- RaiiListenableListener.withLifecycle(RaiiLifecycleAware lifecycleAware, {required T listenable, required VoidCallback onListen, String? debugLabel})
-
Creates a RaiiListenableListener and attaches it to the provided
lifecycleAware.
Properties
- debugLabel → String?
-
Optional label for debugging purposes.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- isDisposed → bool
-
Whether this lifecycle has been disposed.
no setterinherited
- listenable → T
-
The listenable object to which the listener will be attached.
final
- onListen → VoidCallback
-
The callback that will be executed when the listenable notifies its listeners.
final
- raiiHolder → RaiiLifecycleAware?
-
The holder that manages this lifecycle.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
clearRaiiHolder(
) → void -
Clears the holder reference.
inherited
-
disposeLifecycle(
) → void -
Disposes of any resources held by this object.
override
-
initLifecycle(
) → void -
Initializes the lifecycle of this object.
override
-
isLifecycleMounted(
) → bool -
Returns whether this object's lifecycle is currently mounted.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
setRaiiHolder(
RaiiLifecycleAware holder) → void -
Sets the holder for this lifecycle.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited