RtState
The RtState class is a foundational class in the Reactter framework, designed to manage state in a Dart application.
It provides a structured way to create, update, and dispose of state, ensuring that state changes are tracked and propagated efficiently.
This class is typically extended to create custom state that can be used within the Reactter ecosystem.
Syntax
abstract class RtState<E extends RtState<E>> { String? get debugLabel; Map<String, dynamic> get debugInfo; void update(Function? fnUpdate) void notify() void bind(Object instance) void unbind() void dispose()}Properties & Methods
debugLabel: A string that represents the label of the state object for debugging purposes.debugInfo: A map that contains debug information about the state object.
-
update: Executes a callback function and notify its observers that the state has changed. When it is invoked, it emits two lifecycle events to signal the state transition:Lifecycle.willUpdateis emitted first, indicating the impending update.Lifecycle.didUpdateis emitted once the update process is complete.
-
notify: Forces the state to notify its observers. Unlikeupdate, it emits only theLifecycle.didUpdateevent, as it doesn’t involve any preparatory steps before the notification. -
bind: Establishes a connection between the state and a specific instance. This connection allows the instance to reactively update based on changes to the state. By binding the state, the instance becomes aware of changes to the state and can appropriately reflect those changes in its behavior. -
unbind: Releases the connection between the state and the instance. When unbinding, the instance will no longer receive updates from the state. This can be useful when an instance is no longer actively using the state or when it needs to detach from the state temporarily or permanently. -
dispose: Is responsible for cleaning up the state and any associated observers or resources. Disposing of the state ensures that it is properly released and no longer consumes memory or processing resources unnecessarily.
Usage
Declaration
To create a custom state, extend the RtState class and define the state properties and methods.
Register the state using Rt.registerState or as a dependency within the Reactter framework, e.g.:
1class MyState extends RtState<MyState> {2 int _value = 0;3 int get value => _value;4 set value(int n) {5 if (n == _value) return;6 update(() => _value = n); // set new value and notify observers7 }8
9 MyState([int value = 0]) : _value = value;10}11
12final state = Rt.registerState<MyState>(() => MyState()); // Register state8 collapsed lines
13
14Rt.on(15 state,16 Lifecycle.didUpdate,17 (_, __) => print('State updated: ${state.value}')18);19
20state.value = 42; // Calls update internallyUpdating the state
To update the state, call the update method and provide a callback function that modifies the state, e.g.:
1class MyState extends RtState<MyState> {2 collapsed lines
2 int _value = 0;3 int get value => _value;4 set value(int n) {5 if (n == _value) return;6 update(() => _value = n); // set new value and notify observers7 }6 collapsed lines
8
9 MyState._([int value = 0]) : _value = value;10
11 factory MyState(int value) {12 return Rt.registerState(() => MyState._(value)); // Register state13 }14}15
16Rt.on(17 state,18 Lifecycle.didUpdate,19 (_, __) => print('State updated: ${state.value}')20);21
22state.value = 42; // Calls update internallyListening to changes
When a state is updated, it emits the following lifecycle events:
Lifecycle.willUpdateevent is triggered before the callback function ofupdatemethod have been executed.Lifecycle.didUpdateevent is triggered after the callback function ofupdatemethod have been executed or after thenotifymethod have been invoked.
1class MyState extends RtState<MyState> {2 collapsed lines
2 int _value = 0;3 int get value => _value;4 set value(int n) {5 if (n == _value) return;6 update(() => _value = n); // set new value and notify observers7 }6 collapsed lines
8
9 MyState._([int value = 0]) : _value = value;10
11 factory MyState(int value) {12 return Rt.registerState(() => MyState._(value)); // Register state13 }14}15
16Rt.on(17 state,18 Lifecycle.didUpdate,19 (_, __) => print('State updated: ${state.value}')20);21
22state.value = 42; // Calls update internally