Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple ProviderListener #166

Closed
yiss opened this issue Sep 30, 2020 · 8 comments
Closed

Multiple ProviderListener #166

yiss opened this issue Sep 30, 2020 · 8 comments
Labels
enhancement New feature or request

Comments

@yiss
Copy link
Contributor

yiss commented Sep 30, 2020

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 yiss added enhancement New feature or request needs triage labels Sep 30, 2020
@yiss
Copy link
Contributor Author

yiss commented Sep 30, 2020

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
Copy link

This issue may same as #163

@smiLLe
Copy link

smiLLe commented Oct 1, 2020

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
Copy link

smiLLe commented Oct 1, 2020

Or why dont you just nest ProviderListeners?

@yiss
Copy link
Contributor Author

yiss commented Oct 1, 2020

@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
Copy link

erf commented Jan 11, 2021

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
Copy link
Owner

Closing in favor of #335

@bhanuka96
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants