Reputation: 905
This is a part of code for run app:
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (BuildContext context) => _number != null?ChangeNotifierProvider(
create: (context)=>CartList(),
child: HomeScreenDemo(),
):Login())));
I've also tried using MultiProvider instead of ChangeNotifierProvider but it didn't work.
This is the part of Code for home Screen:
Widget build(BuildContext context) {
return Consumer<CartList>(
builder: (context,CART,child){
return Scaffold(
key: scaffoldKey,
backgroundColor: Colors.black,
)
This is how I'm Navigating to checkOut Screen:
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => CartScreen(userPin: _userPin,)
));
This is the code of checkOut Screen:
Widget build(BuildContext context) {
return Builder(builder: (BuildContext context)=>Consumer<CartList>(
builder: (context,cart,child){
return Scaffold(
backgroundColor: Colors.black,
I've tried removing builder before Consumer But it didn't work.
This Is the ScreenShot of error:
Here I'm pushing the route:
showCart? Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),color: Colors.cyan,
),
child: Material(
color: Colors.transparent,
child: InkWell(
splashColor: Colors.black12,
onTap: () {
// Cart OnClick
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => ChangeNotifierProvider.value(
value: Provider.of<CartList>(context, listen: false),
child: CartScreen(userPin: _userPin,)
)
));
},
child: Container(
width:MediaQuery.of(context).size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(CART.count<=1?CART.count.toString()+" ITEM":CART.count.toString()+" ITEMS",style: TextStyle(
letterSpacing: 1.5,fontWeight: FontWeight.w400
),),
SizedBox(height: 5,),
Text(" ₹"+CART.totalPrice.toString().replaceAll(regex, "")),
],
),
SizedBox(width: 160,),
Text('Cart ',style: TextStyle( fontSize: 18,letterSpacing: .5,fontWeight: FontWeight.w400),textAlign: TextAlign.end,),
Icon(Icons.arrow_right,size: 30,),
],
),
padding: EdgeInsets.all(10),
height: 60,
),
),
),
),
),
):Container()
Upvotes: 0
Views: 88
Reputation: 5601
Check the widget tree in Android Studio or VS and you will see that when pushing a route it starts fresh with only MaterialApp (Unless you create the Provider before the MaterialApp) above so there is no CartList to consume, wrap it in a ChangeNotifierProvider.value to expose a previous created provider
Navigator.of(context).push(MaterialPageRoute(
builder: (_) => ChangeNotifierProvider.value( //this context change its name to something else to avoid confusion
value: Provider.of<CartList>(context, listen: false),
child: CartScreen(userPin: _userPin,)
)
));
Upvotes: 1