无法在 flutter 中过滤 listview.builder 的列表

Not able to filter the list for listview.builder in flutter

我在使用 where 执行以下调用时尝试过滤 JSON 数据,但出现错误

List filterproducts=[];
    filterproducts.addAll(widget.bomdatareceived[0]['bom_items'][0]['proc_code'].where((element) => element.contains(widget.processCode)).toList());

错误

    Class 'String' has no instance method 'where'.
Receiver: "61"
Tried calling: where(Closure: (dynamic) => dynamic)

widget.proc_code61 例如下面的 JSON 数据

JSON数据

"bom_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",
                   }
                   ]

上面的 JSON 数据在列表中,我想在这个 ListView.builder 中实现一个过滤器之类的东西,只征集那些具有相同 proc_code 的元素,我我没有在屏幕上实现任何搜索栏之类的东西。我从上一个屏幕收到 proc_code 的值,并希望构建具有相同 proc_code.

的列表

请指导我如何解决这个问题。

您需要执行以下操作:

void main() {
final recievedItems =  { 
              "bom_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 filterproducts=[];
    filterproducts.addAll(recievedItems['bom_items'].where((element) => element.containsValue("61")).toList());
  print(filterproducts);  
}

这将 return 包含 proc_code : 61 的项目:

[{proc_code: 61, name: SPINNING, bom_catalog_item: 327}, {proc_code: 61, name: SPINNING, bom_catalog_item: 390}]

https://dartpad.dev/6e5efe3339d6ca62e2f42c557dfda012