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

Flutter搜索功能问题:ListView Builder无结果及Product属性报错求助

问题分析与解决方法

嘿,我帮你找出了两个核心问题,咱们一步步来修复:

1. 拼写错误导致的Getter不存在问题

你遇到的Class 'Product' has no instance getter 'productNameb'错误,是因为在ListView的itemBuilder里写错了属性名:

// 错误写法:多了一个末尾的b
return ListTile(title: Text(list[index].productNameb));

你的Product模型类里定义的属性是productName,所以把代码改成下面这样就能解决这个报错:

// 正确写法
return ListTile(title: Text(list[index].productName));

2. 显示Instance of Product的问题

如果之前你不小心直接使用Text(list[index])来展示数据,就会出现这个情况——因为直接打印Dart对象实例时,默认会输出Instance of [类名]。不过修复上面的拼写错误后,用Text(list[index].productName)就能正确显示商品名称了。

额外优化建议

还有个小细节需要注意:你的ProductProvider的search方法每次调用时,没有清空之前的搜索结果列表,多次搜索后旧数据会和新数据混在一起。建议在search方法开头添加清空逻辑:

void search(name) async {
  try {
    products.clear(); // 新增:清空旧结果
    var url = 'http://10.0.2.2:8000/api/user/search/$name';
    var response = await http.get(url);
    var data = jsonDecode(response.body);
    for (var x in data) {
      var list = Product(
          id: x['id'],
          productName: x['productName'],
          categoryId: x['categoryId'],
          price: x['price'],
          sale: x['sale']);
      products.add(list);
    }
  } catch (e) {
    print(e);
  }
  notifyListeners();
}

内容的提问来源于stack exchange,提问作者Sermed mayi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:50:00