这是运行应用程序代码的一部分:
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (BuildContext context) => _number != null?ChangeNotifierProvider(
create: (context)=>CartList(),
child: HomeScreenDemo(),
):Login())));
我还尝试了使用MultiProvider而不是ChangeNotifierProvider,但是没有用。
这是主屏幕代码的一部分:
Widget build(BuildContext context) {
return Consumer<CartList>(
builder: (context,CART,child){
return Scaffold(
key: scaffoldKey,
backgroundColor: Colors.black,
)
这是我导航到Checkout屏幕的方式:
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => CartScreen(userPin: _userPin,)
));
这是签出屏幕的代码:
Widget build(BuildContext context) {
return Builder(builder: (BuildContext context)=>Consumer<CartList>(
builder: (context,cart,child){
return Scaffold(
backgroundColor: Colors.black,
我曾尝试在Consumer之前删除构建器,但这没有用。
这是错误的屏幕截图:
我在这里推路线:
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()
答案 0 :(得分:1)
在Android Studio或VS中检查小部件树,您会看到,在推送路由时,它仅从上方的MaterialApp重新开始(除非您在MaterialApp之前创建了Provider),所以没有CartList可供使用,将其包装在ChangeNotifierProvider.value公开以前创建的提供程序
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,)
)
));