Inmutable States
Meedu is designed to streamline state management for both simple and complex apps, emphasizing the use of immutable state.
note
#
With equatableAdd equatable as a dependency in your pubspec.yaml
file
Now you can create a Class to manage your state
#
With freezedTo use freezed you need build_runner and freezed_annotation
in your pubspec.yaml
file (replace latest_version
with the latest version of each dependency)
Now you can use the LoginState
class to create a StateNotifier
info
If you only want to update the state of your StateNotifier
but you don't want to notify to the listeners (don't rebuild the Consumer
widgets)
you can use the onlyUpdate
method in your StateNotifier
.
Next you need to create a StateNotifierProvider
and use the Consumer
widget to listen the changes in your state
Consider the following Login Form UI, where the ElevatedButton
is enabled only when both the email and password in our state are not empty.
#
ConsumerWidgetAlso you can extend from ConsumerWidget
to create a widget and listen the changes in your notifier
NOTE
The ref
parameter in a Consumer
or a ConsumerWidget
can be used to listen multiples providers.
#
Listen to changes in our notifiers without rebuilding the UISometimes we need to listen the changes in our state and perform some actions like showing a dialog, snackback, or navigation. In those cases, you can use ref.listen
inside a Consumer
or a ConsumerWidget
.
note
ref.listen
only listens the changes in our state but does not rebuild our Consumer
or ConsumerWidget
.