You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter使用Provide状态管理购物车报错:String不是Item子类型

解决"type 'String' is not a subtype of type 'Item'"错误

这个错误的根源非常直白:你的Cart类的add方法明确要求传入一个Item类型的对象,但在Quantities页面的"Add to Cart"按钮点击事件里,你传入的是productname——这是个字符串类型,自然会触发类型不匹配的报错。

快速修复(基于现有代码结构)

直接在Quantities.dart的按钮点击逻辑里,用页面接收的参数组装成一个完整的Item对象,再传入购物车的add方法:

修改Quantities.dart中的onPressed代码:

onPressed: () {
  // 用页面接收的参数创建Item实例,再传入add方法
  cart.add(Item(
    title: productname,
    price: productprice,
    image: productimage,
  ));
},

更优雅的优化方案

其实你可以直接把整个Item对象传递到Quantities页面,避免拆分多个参数传递,代码会更简洁易维护:

  1. 修改HomePage中的跳转代码
onTap: (){
  Navigator.of(context).push(MaterialPageRoute(builder: (context) => Quantities(
    item: _product[index], // 直接传递完整的Item对象
  )));
},
  1. 重构Quantities页面
class Quantities extends StatefulWidget {
  final Item item; // 直接接收Item对象
  Quantities({required this.item}); // Dart 2.12+ 请添加required关键字

  @override
  _QuantitiesState createState() => _QuantitiesState(item);
}

class _QuantitiesState extends State<Quantities> {
  final Item item;
  _QuantitiesState(this.item);

  @override
  Widget build(BuildContext context) {
    return Consumer<Cart>(
      builder: (context,cart,child){
        return PlatformScaffold(
          appBar: PlatformAppBar(
            backgroundColor: Colors.lightBlue[900],
            title: Text('Details'),
          ),
          body: ListView(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(top: 20.0),
                child: Container(
                  child: Column(
                    children: <Widget>[
                      Container(
                        height: 150.0,
                        child: GridTile(
                          child: Container(
                            color: Colors.white,
                            child: Image.asset(item.image),
                          ),
                        ),
                      ),
                      Text(item.title,style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),),
                      Text("Price: "+item.price.toString()+" SAR",style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),),
                      Padding(
                        padding: const EdgeInsets.only(top: 20.0),
                        child: Center(
                          child: PlatformButton(
                            onPressed: () {
                              cart.add(item); // 直接传入Item对象,类型完全匹配
                            },
                            child: Text('Add to Cart',style: TextStyle(color: Colors.white),),
                            androidFlat: (_) => MaterialFlatButtonData(
                                color: Colors.cyan),
                            ios: (_) => CupertinoButtonData(
                                color: Colors.cyan
                            )),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}

这样修改后,不仅彻底解决了类型不匹配的错误,代码结构也更清晰,后续维护起来会更省心。

内容的提问来源于stack exchange,提问作者mohammed nabil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 11:42:54