rrousselGit / riverpod

A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.
https://riverpod.dev
MIT License
6.28k stars 955 forks source link

Multiple ProviderListener #166

Closed yiss closed 3 years ago

yiss commented 4 years ago

ProviderListener is really good the only problem is that it only listen to one provider at time. my question is that would there be a solution for having a MultipleProviderListener where it listens to multiple provider and fires the onchange callback every-time one of these providers is changed.

Example :

return MultipleProviderListener(
providers: [authProvider, roleProvider],
onchange: (context, values) {
// here the value of authProvider is going to be values[0]
// and the value of roleProvider is going to be values[1]
// do my business logic
}
child: Container()
)

Is this a something that can be achieved ? and how is the performance going to be affected ? Thank you again!

yiss commented 4 years ago

Another proposition for it could be :

final listener1 = ProviderListener(
  provider: provider1,
  onChange: (context, value) {},
);

final listener2 = ProviderListener(
  provider: provider2,
  onChange: (context, value) {},
);

final listener3 = ProviderListener(
  provider: provider3,
  onChange: (context, value) {},
);

return MultipleListener(
    listeners: [listener1, listener2, listener3], child: Container());
quangson91 commented 4 years ago

This issue may same as https://github.com/rrousselGit/river_pod/issues/163

smiLLe commented 4 years ago

Have a look at the hooks implementation useProvider.

Widget build(context) {
  final fooState = useProvider(foo.state);
  final barState= useProvider(bar.state);
  final bazState = useProvider(baz.state);

  return Container();
}
smiLLe commented 4 years ago

Or why dont you just nest ProviderListeners?

yiss commented 4 years ago

@quangson91 Thank you for pointing that out, I didn't see it. But yeah useProviderListener would solve this for me since I use Hooks, but the problem is that some people use river_pod without Hooks. @smiLLe use provider would not suffice here cause you'll have to manage provider change manually for every provider and again not all the people use hooks, nesting ProviderListeners is the current solution but it really gets messy after three nested providers

erf commented 3 years ago

This sounds similar to #67. I made a comment on this here with a link to a gist where i make a new provider by adding a list of providers and combine them. I'm quite new to riverpod so not sure if this is a good solution though..

rrousselGit commented 3 years ago

Closing in favor of #335

bhanuka96 commented 3 years ago

I am waiting for this feature. So, I am currently using this way. It's working for me.

    ProviderListener(
                            provider: authNotifierProvider,
                            onChange: (context, state) {
                            },
                            child: ProviderListener(
                              provider: tenantCatLogNotifierProvider,
                              onChange: (BuildContext context, value) {

                              },
                              child: Consumer(builder: (context, watch, child) {
                                final state = watch(tenantCatLogNotifierProvider);
                                final authState = watch(authNotifierProvider);
                                return IgnorePointer(
                                    ignoring: state is TenantCatLogLoading ||  authState is AuthLoading,
                                    child:);
                              }),
                            )
                          ),

@rrousselGit Hey, can you give me this way is okay or it is bad practice?