Flutter Provider -Adding Proxy Provider that has dependency on Stream Provider

Viewed 1072
MultiProvider(
    providers: [
         // In this sample app, CatalogModel never changes, so a simple Provider
     // is sufficient.
      Provider(create: (context) => CatalogModel()),
      // CartModel is implemented as a ChangeNotifier, which calls for the use
         // of ChangeNotifierProvider. Moreover, CartModel depends
     // on CatalogModel, so a ProxyProvider is needed.
     ChangeNotifierProxyProvider<CatalogModel, CartModel>(
         create: (context) => CartModel(),
         update: (context, catalog, cart) {
          cart.catalog = catalog;
          return cart;
          },
     ),
   ],)

I want to replace the Provider widget with the Stream Provider Widget as i'm working with the streams.How Can i have the same changenotifierproxy (Cart Model) provider that depends on the Stream Provider instead of Provider.

1 Answers

I think you can try to use the builder property of MultiProvider to obtain a new BuildContext that has access to the previous provider builder (so that you can call Provider.of<CatalogModel>.yourStreamValue for example).

MultiProvider(
  providers:[Provider(create:(context)=>CatalogModel()],
  builder: (context, child) => StreamProvider<CartModel>.value(
     initialData: null,
     value: Provider.of<CatalogModel>.yourStreamValue,
     child: child,
  )
  child: child,

)

Related