-
-
Notifications
You must be signed in to change notification settings - Fork 500
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
MultiProvider and two or more ChangeNotificationProviders #63
Comments
@rrousselGit Please answer me this question, thank you for your time: Let's say I instantiate Foo in the provider: Provider(builder: (context)=>Foo()); If I want to use that initialized Foo() in a class (without context), should I first make it a singleton or static, then pass it to provider? Is there any helpers on the mixin ChangeNotifier that will transform the class in a singleton? I think that will be a nice feature if you want to build service classes. |
I don't understand the question |
Example: |
Don't make singletons/globals. The point is this library is to purposefully avoid them. |
Yes, that's why I got so confused... I don't want to use Singletons but I'm not getting how I can access the provider class outside the builder. The only way I found is to use it on didChangeDependencies() but I want to avoid logic inside the UI Picking again the basic example above of Counter and SwitchTheme, both with ChangeNotifier. |
That, or using Widgets are not UI. They are metadata. |
If I pass the context like Counter(context) and use Provider.of inside , will it work? |
That's not recommended. But again, the use-case you're describing is a use-case for #46. And then do something like: MultiProvider(
providers: [
ChangeNotifierProvider<Counter>(builder: (context) => Counter(0)),
ProxyProvider<Counter, ThemeSwitch>.custom(
builder: (context, counter, previous) {
final theme = previous ?? ThemeSwitch(ThemeState.light);
theme.themeState =
(counter.value > 5) ? ThemeState.dark : ThemeState.light;
return theme;
},
dispose: (context, value) => value.dispose(),
providerBuilder: (_, value, child) =>
ChangeNotifierProvider.value(notifier: value, child: child),
),
],
) |
Great ! Thank your for your help/time! I'll appreciate |
The app works like this:
It has a Counter with ChangeNotifier and a ThemeSwitch also with ChangeNotifier
if number > 5 it changes to dark.
Consumer2 is used ( the logic for switching is made inside Consumer2).
I have this question too: How can I have access to Counter value inside the ThemeSwitch class to make the logic there.
The error happens when one of them is rebuilding the same descendants when the other is rebuilding at the same time.
Error:
Exception has occurred. FlutterError (setState() or markNeedsBuild() called during build. This ChangeNotifierProvider<ThemeSwitch> widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase. The widget on which setState() or markNeedsBuild() was called was: ChangeNotifierProvider<ThemeSwitch>(state: _ListenableProviderState<ThemeSwitch>#e36d8) The widget which was currently being built when the offending call was made was: Consumer2<ThemeSwitch, Counter>(dirty, dependencies: [_Provider<ThemeSwitch>, _Provider<Counter>]))
Files:
The text was updated successfully, but these errors were encountered: