Error: Could not find the correct Provider<CartList> above this Consumer<CartList> Widget in flutter
Error: Could not find the correct Provider<CartList> above this Consumer<CartList> Widget in flutter
这是 运行 应用的部分代码:
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,
)
这就是我导航到结帐屏幕的方式:
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()
检查 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,)
)
));
这是 运行 应用的部分代码:
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,
)
这就是我导航到结帐屏幕的方式:
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()
检查 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,)
)
));