【问题标题】:How to implement `where` to filter data in flutter list?如何实现`where`过滤flutter列表中的数据?
【发布时间】:2021-01-01 00:01:07
【问题描述】:

我是学习flutter的新手,我正在尝试在List上实现where来过滤数据,但我完全错了,请指导我正确的实现

实施

 List filterproducts=   productslist.where(widget.bomdatareceived[0]['purchase_items'][0]['product_code'].contains(widget.product_code));

错误

type 'bool' is not a subtype of type '(dynamic) => bool'

JSON 数据

"purchase_items": [
               {
                     
                   "product_code": "61",
                   "name": "SPINNING",
                   "bom_catalog_item": "327",
                  },
                  {
                     
                   "product_code": "61",
                   "name": "SPINNING",
                   "bom_catalog_item": "390",
                   },
                   {
                     
                    "product_code": "65",
                    "name": "DYING",
                    "bom_catalog_item": "1056",
                   }
                   ]

【问题讨论】:

  • 小部件是什么意思?一个常数?
  • @Merym 是的,widget.product_code 是一个字符串值

标签: flutter filter flutter-listview


【解决方案1】:
List filterproducts= productslist.where((f) => f.bomdatareceived[0]['purchase_items'][0]['product_code'].contains(widget.product_code)).toList();

【讨论】:

  • 嗨,我试过你的答案,它给了我 'WhereIterable' 类型的错误不是 'List' 类型的子类型,请进一步指导,或者我应该提供更多数据,比如 JSON 数据结构?
  • @Harry West 你试过了吗?
  • 是的,我试过了,但没有解决问题,它给了我错误The method 'where' was called on null. Receiver: null Tried calling: where(Closure: (dynamic) => dynamic) ,如果需要更多信息,请告诉我
  • 你能打印这个产品列表吗
  • 我已经添加了 JSON 数据,这必须实现
【解决方案2】:
List filterproducts=   productslist.where((i) => widget.bomdatareceived[0]['purchase_items'][0]['product_code'].contains(widget.product_code));

【讨论】:

  • 嗨,我试过你的答案,它给了我type 'WhereIterable<dynamic>' is not a subtype of type 'List<dynamic>'的错误,请进一步指导
【解决方案3】:
var filterproducts=[];
filterproducts.addAll(widget.bomdatareceived[0]['purchase_items'][0]['product_code']
        .where((element) => element.contains(widget.product_code))
        .toList());

【讨论】:

  • 嗨,我实施了你的建议,它给了我这个错误,方法'where'在null上被调用。接收者:null 尝试调用:where(Closure: (dynamic) => dynamic)
  • filterproducts.addAll(purchase_items .where((item) => item["proc_code"] ==widget.product_code) .toList());
【解决方案4】:

结果:

代码:

  var purchase_items = [
               {
                     
                   "proc_code": "61",
                   "name": "SPINNING",
                   "bom_catalog_item": "327",
                  },
                  {
                     
                   "proc_code": "61",
                   "name": "SPINNING",
                   "bom_catalog_item": "390",
                   },
                   {
                     
                    "proc_code": "65",
                    "name": "DYING",
                    "bom_catalog_item": "1056",
                   }
                   ];
 List<Map<String,String>> filterproducts = new List<Map<String,String>>();


filterproducts = purchase_items.where((f) => f['proc_code'] == ('61')).toList();
  print(filterproducts);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多