【问题标题】:Flutter : How check if List contain passes item?Flutter:如何检查列表是否包含通过项?
【发布时间】:2021-03-22 04:13:49
【问题描述】:

按下图标时我有方法,

onPressed: () {
          check(snapshot.data[index]);
        },

以及检查数据是否在另一个列表中找到的方法,我做了如下:

    void add_cart(item){
    for (var x in list){
      if (x.id==item.id){
        cartCount = 1;
      }
      else {
        cartCount = 0;
      }
    }

    if (cartCount == 1){
      list.remove(item);
    }
    else{
      list.add(item);
    }
    print(cartCount);
    notifyListeners();
  }

cartcount 工作正确,但 remove 方法不起作用,真正的方法是什么?

【问题讨论】:

  • 您是否拼错了 llist (double ll) 。在您上面的代码中 list.remove(item);不同于 llist.add(item);
  • @bluenile 我会尽快更新它
  • @bluenile 我改变了一些东西
  • 放你的商品型号代码

标签: list flutter object


【解决方案1】:

您需要像这样覆盖您的购物车模型的 ==hashcode

class CartModel {
  String id;

  // other properties

  @override
  bool operator ==(Object other) => other is CartModel && runtimeType == other.runtimeType && id == other.id;

  @override
  int get hashCode => id.hashCode;
}

之后list.remove(item); 将按预期工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    相关资源
    最近更新 更多