I have three ChangeNotifierProvider
providers, let's call them A
, B
and C
.
What I need is for changes in providers A
and B
to update provider C
, but the code below isn't working.
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => A()),
ChangeNotifierProvider(create: (context) => B()),
ChangeNotifierProxyProvider<A, C>(
create: (context) => C(),
update: (_, a, c) => c!..updateFromA(a),
),
ChangeNotifierProxyProvider<B, C>(
create: (context) => C(),
update: (_, b, c) => c!..updateFromB(b),
),
]
I think the reason why it doesn't work is because I end up with multiple copies of provider C
, but I am not sure how to fix it or what a better alternative implementation would look like.
After doing some searching, I realized that Flutter provides an elegant out-of-the-box way of solving this problem using
ChangeNotifierProxyProvider2
.Flutter also provides
ChangeNotifierProxyProvider3
throughChangeNotifierProxyProvider6
, but that would, of course, get very verbose and confusing.