Signal
Signal is reactive state that encapsulate a value changing over time. When the value of a signal changes, it automatically notifies its observers.
Syntax
Signal<T>(T initialValue); Signal accepts this parameter:
initialValue: Initial value ofTtype that the it will hold.
Properties & Methods
Signal provides the following properties and methods:
-
value: A getter/setter that allows to read and write its state. -
Various properties and methods depending on the type of
T. For example, ifTis aList, you can use the methods and properties of theListclass:final listSignal = Signal<List<int>>([]);listSignal.addAll([1, 2, 3]);listSignal.removeAt(0);listSignal.clear();
Properties and methods inherited from RtState
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
Signal can be initialized using the constructor class:
final intSignal = Signal<int>(0);final strSignal = Signal("initial value");final userSignal = Signal(User());Reading and writing the value
Signal has a value property that allows to read and write its state:
intSignal.value = 10;print("Current state: ${intSignal.value}");or also can use the callable function:
intSignal(10);print("Current state: ${intSignal()}");or simply use .toString() implicit to get its value as String:
print("Current state: $intSignal");Updating the value
Use update method to notify changes after run a set of instructions:
userSignal.update((user) { user.firstname = "Firstname"; user.lastname = "Lastname";});Use notify method to force to notify changes.
userSignal.notify();Listening to changes
When value has changed, the Signal will emit the following events(learn about it here):
Lifecycle.willUpdateevent is triggered before the change invalueorupdatemethod have been invoked.Lifecycle.didUpdateevent is triggered after the change invalueor afterupdateornotifymethods have been invoked.
Example of listening to changes:
1Rt.on(2 mySignal,3 Lifecycle.didUpdate,4 (_, state) => print("State value has changed to: ${state.value}"),5);