I'm using the Provider library in Flutter, and I've got a ChangeNotifier
which notifies its listeners when the application state changes.
I have two ProxyProvider
s which should update their dependent objects when that happens. Except only the first one executes. The second never does.
Widget providerSetup({required Widget child}) {
return MultiProvider(
providers: [
ChangeNotifierProvider<AppStateNotifier>.value(value: AppState.notifier),
ProxyProvider<AppStateNotifier, AppState>(
create: (context) => AppState.state,
update: (context, n, child) => n.value, // this executes
),
ProxyProvider<AppStateNotifier, RecipeCustomization?>(
create: (context) => AppState.state.custom,
update: (context, n, child) => n.value.custom, // this does not
),
],
child: child,
);
}
How can I fix this?
Because each Each ProxyProvider
gives you the previous value of its dependency, it won't change the state when you listen to AppStateNotifier
twice.
ProxyProvider<AppStateNotifier, AppState>(
update: (context, n, child) => n.value,
),
ProxyProvider<AppState, RecipeCustomization?>(
update: (context, state, child) => state.custom,
),