SimpleNotifier
Just create a class that extends of SimpleNotifier
#
SimpleProviderNow you need to create a provider
as a global variable using the SimpleProvider
class.
note
If you don't use lambda functions to define the callback for your provider you must define the Generic Type
Now you can use the Consumer
widget to read your CounterController
.
By default the counterProvider
variable doesn't create one instance of CounterController
until it is need it. In this case the Consumer
widget call to the read
property of our counterProvider
and check if the CounterController
was created and return the CounterController
that was created before or create a new CounterController
.
The dispose
method in our CounterController
will be called when the route
who created the CounterController
is popped.
If you don't want to call to the dispose
method when the route
who created the CounterController
is popped you could use.
note
autoDispose: false
can be used to define global states.
WARNING
When you disable the autoDispose
of your provider
you need to handle it manually. For example
#
Listen the changes in your ControllerYou could use the ProviderListener
Widget to listen the changes in our CounterController
note
if you want to listen multiples providers at the same time you can use
the MultiProviderListener
widget.
note
ProviderListener
andMultiProviderListener
don't rebuild the widget returned by thebuilder
method.
Or you can listen the changes in your SimpleProvider as a StreamSubscription
#
Avoid rebuilds using the .ids and .select methodsIf you have multiples Consumer
widgets in your Views and you only want rebuild certain Consumer you can use the .ids
and .select
methods.
IMPORTANT
The .ids
filter is deprecated in favor to .select
and it will be removed
in flutter_meedu:^6.x.x
If you don't want to use ids
to rebuild your Consumer
you can use the select
method.
The next code rebuilds the first Consumer
only when the counter is highest than 5.
note
Also you can use the select
method to listen when a value has changed and rebuild your Consumer.
The next code rebuild your Consumer
only when the counter
value has changed in your CounterController.
If you want direct access to the value returned by counterProvider.select((_) => _.counter)
you can use ref.select
IMPORTANT: the .ids
filter only should be used with ref.watch
.
note
- You can use the filters
.select , .when , .ids
in yourProviderListener
or in yourMultiProviderListener
Example:
#
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.