UseState
UseState
is a hook that allows to declare state variables and manipulate its value
, which in turn notifies about its changes to listeners.
Syntax
UseState<T>(T initialValue, { String? debugLabel });
UseState
accepts these parameters:
initialValue
: Initial value ofT
type that it will hold.debugLabel
: (optional) A label to identify the hook in the DevTools extension.
Properties & Methods
UseState
provides the following properties and methods:
value
: A getter/setter that allows to read and write its state.
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.willUpdate
is emitted first, indicating the impending update.Lifecycle.didUpdate
is emitted once the update process is complete.
-
notify
: Forces the state to notify its observers. Unlikeupdate
, it emits only theLifecycle.didUpdate
event, 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
UseState
can be initialized using the constructor class:
final intState = UseState<int>(0);final strState = UseState("initial value");final userState = UseState(User());
Reading and writing the value
UseState
has a value
property that allows to read and write its state:
intState.value = 10;print("Current state: ${intState.value}");
Updating the state
Use update
method to notify changes after run a set of instructions:
uState.update((value) { uState.value = "New value";});
Use notify
method to force to notify changes.
userState.notify();
Listening to changes
When value
has changed, the UseState
will emit the following events(learn about it here):
Lifecycle.willUpdate
event is triggered before the change invalue
orupdate
method have been invoked.Lifecycle.didUpdate
event is triggered after the change invalue
or afterupdate
ornotify
methods have been invoked.
Example of listening to changes:
1Rt.on(2 myState,3 Lifecycle.didUpdate,4 (_, state) => print("State value has changed to: ${state.value}"),5);