list - 编辑现有列表物品
问题描述
我想编辑列表,但我不知道如何实现。
我在下面粘贴了我的代码
Container(
child: ListView.builder(
itemCount: filterList.length,
shrinkWrap: true,
itemBuilder: (BuildContext context, int intex) {
// return MenuList(filterList[intex]);
return Container
(
height:75,
//color: Colors.blue,
child: Card(
elevation: 1.0,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(3.0)),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left:8.0),
child: Container(
child: Image(image:AssetImage("images/beef_burger.jpg"),
width: 70,height: 50,),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width/2.2,
// color: Colors.blue,
child: Text(filterList[intex].itemName,maxLines: 1,),
),
Container(
child: Text("AED ${filterList[intex].itemPrice}",style: TextStyle(color: Colors.grey),),
),
],
),
),
)
],
),
),
Container(
// color: Colors.red,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
width: 50,
height: 50,
// color: Colors.blue,
child: FlatButton(onPressed: (){
if(filterList[intex].counter > 0){
setState(() {
filterList[intex].counter--;
});
}
}, child: Image(image: AssetImage("images/minus.png"),width: 20,height: 20,),),
),
Container(
child: Text("${filterList[intex].counter}"),
),
Container(
width: 50,
height: 50,
// color: Colors.green,
child: FlatButton(onPressed: () async{
setState(() {
filterList[intex].counter++;
});
cartItemList.add({"itemObj":filterList[intex],"quantity":filterList[intex].counter});
// cartItemList = [{"index":widget.intex,"itemObj":widget.items,"quantity":_itemCount}];
print(addedItems(cartItemList));
final prefs = await SharedPreferences.getInstance();
await prefs.setStringList('itemList', addedItems(cartItemList));
}, child: Image(image: AssetImage("images/plus.png"),width: 20,height: 20,),),
),
],
),
),
],
),
),
);
}),
),
我已将列表转换为列表以存储在共享首选项中
List<String> addedItems(List<dynamic> cartList){
try {
var res = cartList.map((v) =>
json.encode(v)).toList();
listNew = res;
return res;
} catch (err) {
// Just in case
return [];
}
当我将项目添加到购物车列表时,我得到的输出为
- 添加了一项
[{"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001","item_discount":0.0,"item_id":552, "category_id":12},"数量":1}]
- 再次添加相同的项目
[{"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001","item_discount":0.0,"item_id":552, "category_id":12},"quantity":1}, {"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001" ,"item_discount":0.0,"item_id":552,"category_id":12},"quantity":2}]
- 添加了不同的项目
[{"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001","item_discount":0.0,"item_id":552, "category_id":12},"quantity":1}, {"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001" ,"item_discount":0.0,"item_id":552,"category_id":12},"quantity":2}, {"itemObj":{"item_price":20.0,"item_name":"英式早餐","item_img ":" ","item_code":"002","item_discount":0.0,"item_id":71,"category_id":12},"quantity":1}]
但我想要输出,就像我再次添加相同的项目时一样
[{"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001","item_discount":0.0,"item_id":552, "category_id":12},"数量":2}]
解决方案
List 没有内置功能,只能添加独特的项目。您可以使用地图或手动检查该项目是否已存在。
要检查您可以使用 indexWhere 方法: https ://api.flutter.dev/flutter/dart-core/List/indexWhere.html
推荐阅读
- java - 解析时到达文件末尾,找不到丢失的括号
- reporting-services - 我们可以将其他报告文件作为附件与 SSRS 订阅报告一起发送吗?
- android - 如何在 kotlin 中实现折叠工具栏
- typescript - 赛普拉斯 - 将登录功能从 before / beforeEach 挂钩移动到单独的文件
- html - Bemit : 扩展公约
- routes - 为什么在服务器上部署后 Blazor WASM 路由不使用正确的基本 URI?(NavigationManager.NavigateTo() 方法)
- java - SpringBoot如何开启History模式
- java - 在 Tomcat 中调试 Vaadin 14
- python - 将 JSON 请求转换为数据框中的一行
- javascript - 属性 X 在 App 类型上不存在