在用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)
CCD_ 2是CCD_
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);
}
这将返回包含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