flutter - 如何根据 Flutter 中的 id 从列表中查找项目的长度?
问题描述
我有一个列表 List product=[];
产品=等待 dbHelper.getProducts();
我试图用 sqflite 构建一个购物车,我得到一个列表,相同的产品显示在不同的列表磁贴中,这是我的代码,
product={[{ "product name" : "apple", "product id" : "1", "quantity" : "2",},{ "product name" : "apple", "product id" : "1", "quantity" : "5",},{ "product name" : "orange", "product id" : "2", "quantity" : "10",}]}
ListView.builder(
itemCount: product.length,
itemBuilder: (c,i){
return ListTile(
title:Text(product[i].product_varient_name.toString()),
subtitle: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(product[i].base_price),
Text("qty : "+product[i].varient_id),
Container(
height: 80,width: 80,
child: Image.network("https://v.3multi.qgrocer.in/public/" +product[i].varient_image.toString())),
],
),
);
}),
输出是,
01 Apple 5
02 Apple 2
03 orange 1
...
解决方案
首先,我认为您的变量声明存在问题。
这就是应该使用之前的类型声明它的方式:
Map<String, List<Map<String, String>>> product = {
'basket': [
{
"product name": "apple",
"product id": "1",
"quantity": "2",
},
{
"product name": "apple",
"product id" : "1",
"quantity" : "5",
},
{
"product name": "orange",
"product id" : "2",
"quantity" : "10",
}
]
};
这里的第二个问题是你没有正确使用东西我猜你不需要 aMap<String, List<Map<String, String>>>
但 aMap<String, Map<String, int>>
所以你可以有这个例如:
Map<String, Map<String, int>> product = {
'apple': {
'id': 1,
'quantity': 2,
},
'banana': {
'id': 2,
'quantity': 10,
},
};
现在您有了这个,访问 Map 的元素变得更加容易,因为您可以通过产品名称访问它。
这里我给你做了一个使用例子:
Map<String, Map<String, int>> product = {
'apple': {
'id': 1,
'quantity': 2,
},
'banana': {
'id': 2,
'quantity': 10,
},
'potato': {
'id': 3,
},
};
void addQuantityFromProductName({
String productName = '',
int quantity = 0
}) {
if (productName != '' && product.containsKey(productName))
setState(() {
(product[productName] as Map<String, int>).update(
'quantity',
(existingValue) => existingValue + quantity,
ifAbsent: () => quantity
);
});
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
child: ListView.builder(
shrinkWrap: true,
itemCount: product.length,
itemBuilder: (BuildContext context, int i) {
return ListTile(
onTap: () => addQuantityFromProductName(
productName: product.keys.elementAt(i),
quantity: 10
),
title: Text(
product.keys.elementAt(i)
),
trailing: Text(
product.values.elementAt(i)['quantity'] == null ? "Undefined" :
product.values.elementAt(i)['quantity'].toString()
),
);
}
)
)
);
}
因此,在示例中,当您单击 ListTile 时,您将在产品名称中添加 10 个数量
按下前
按下后
您在这里有一个关于如何访问和更新地图的元素或值的概念(双重地图)
推荐阅读
- .net - 使用 VB.Net 将字符串上传到亚马逊 s3 存储桶
- javascript - 我不明白为什么 JavaScript 中的某个箭头函数的行为不像普通的
- amazon-s3 - kafka/confluent 平台上的最佳方法:“流 S3 文件 -> Java 应用程序中的进程 -> 存储在 Oracle DB 中”
- c - 指针用于存储地址,但在 char *ptr = "abc" 这样的字符串中,如果您打印 'ptr' 为什么打印的是 abc 而不是地址
- ssl - .Net Core 3.1 webapi 托管和 ERR_CONNECTION_RESET
- amazon-web-services - 使用 Carrd.co 和 AWS Route 53 发布自定义域网站
- scala - Spark:替换嵌套列中的空值
- python - BeautifulSoup:如何提取没有标签的文本子元素?
- reactjs - 我在设置功能挂钩状态时遇到问题
- node.js - 通过 express.js 在浏览器中显示警报