StateNotifier
If you have a more complex state consider using the StateNotifier class instead of SimpleNotifier.
A StateNotifier stores a single immutable state.
An immutable state is an instance of one Class that overrides ==
and hashCode
. For example you could use equatable to create an immutable Class.
Add equatable as a dependency in your pubspec.yaml
file
Now you can create a Class to manage your state
Now you can use the LoginState
class to create a StateNotifier
Next you need to create a StateNotifier
and use the Consumer
widget to listen the changes in your state
Also you can use the WatchFilter
class to only rebuild your Consumer
when is need it.
For example the next code only rebuilds the Consumer widget when the email
in our LoginState
has changed.
note
When you use the WatchFilter
to listen the changes in a StateNotifier
you need to define the generic types
in the watch method.
- The first generic type is the class witch extends of StateNotifier.
- The second generic type is the class used to manage our state.
- The
ids
andselect
params only work with aSimpleNotifier
.
info
In the same way you can listen the changes of your StateNofier
using the ProviderListener
widget or using a StreamSubscription
Also the StateNotifier
class allows you to listen when the state is going to change and listen when the state has changed.