RxNotifier<T> class
abstract
An abstract base class for creating reactive notifiers that manage
a state of type T.
The RxNotifier class provides a foundation for creating reactive notifiers
that encapsulate a piece of immutable state and notify their listeners
when the state changes. Subclasses of RxNotifier must override the
initial method to provide the initial state and implement the logic
for updating the state.
Example usage:
class CounterNotifier extends RxNotifier<int> {
@override
int initial() {
return 0; // Initial state
}
void increment() {
state++; // Update the state
}
}
CounterNotifier get counter => Super.init(CounterNotifier());
// Adding a listener to the notifier
counter.addListener(() {
print('Counter changed: ${counter.state}');
});
// Updating the state
counter.increment(); // This will trigger the listener and print the updated state.
It is best used for global state i.e state used in multiple controllers but it could also be used for a single controller to abstract a state and its events e.g if a state has a lot events, rather than complicating your controller, you could use an RxNotifier for that singular state instead.
Note: When using the RxNotifier class, it is important to call the
dispose() method on the object when it is no longer needed to
prevent memory leaks.
This can be done using the onDisable method of your controller.
Constructors
- RxNotifier()
-
An abstract base class for creating reactive notifiers that manage
a state of type
T.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Checks if the reactive object has registered listeners.
no setterinherited
- loading → bool
-
The loading state of the notifier.
no setter
- mounted → bool
-
Verify whether dispose was called or not.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state ↔ T
-
The current state of the reactive object.
getter/setter pairoverride
Methods
-
addListener(
VoidCallback listener) → void -
Adds a listener to the reactive object.
inherited
-
dispose(
) → void -
Disposes the reactive object and releases associated resources.
inherited
-
initial(
) → T - Retrieves the initial state for the notifier.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
removeListener(
VoidCallback listener) → void -
Removes a listener from the reactive object.
inherited
-
toggleLoading(
) → void - Toggles the loading state of the notifier. By default the loading state is false.
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited