Working with tags
If you need to have multiple notifiers using the same StateNotifier
or Bloc
class, but you don't want to create a provider for each notifier, you can use the tag
parameter when you call the read
function of our providers.
Consider the following example:
Now we can use a single Consumer
widget to create two different instances of CounterNotifier
using the same provider.
note
In the example above, we have created 2 instances of CounterNotifier
by using tags, and each of these instances has its own state.
NOTE: If you need to manually dispose of your notifiers that were created using tags, you can use
#
How to Set Arguments for Our NotifiersWhen you call the setArguments function of our StateNotifierTagArgumentsProvider
, you must use the tag argument.
#
Tags and filtersIf you use ref.watch
or ref.listen
with filters like .select
or .when
, the tag
parameter of ref.watch
or ref.listen
will be omitted, as the filters have their own tag
parameter.