如何根据 Flutter 中的 id 从列表中找到项目的长度?
How can I find the length of an item from a list based on its id in Flutter?
我得到了一个列表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",
}
]
};
这里的第二个问题是你没有正确使用东西我猜你不需要Map<String, List<Map<String, String>>>
而是Map<String, Map<String, int>>
所以你可以这样举个例子:
Map<String, Map<String, int>> product = {
'apple': {
'id': 1,
'quantity': 2,
},
'banana': {
'id': 2,
'quantity': 10,
},
};
现在有了这个,访问地图的元素就更容易了,因为您可以通过产品名称访问它。
这里给大家做了一个使用示例:
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 个数量
按下前
按下后
你在这里有一个关于如何访问和更新地图的元素或值的概念(双重地图)
我得到了一个列表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",
}
]
};
这里的第二个问题是你没有正确使用东西我猜你不需要Map<String, List<Map<String, String>>>
而是Map<String, Map<String, int>>
所以你可以这样举个例子:
Map<String, Map<String, int>> product = {
'apple': {
'id': 1,
'quantity': 2,
},
'banana': {
'id': 2,
'quantity': 10,
},
};
现在有了这个,访问地图的元素就更容易了,因为您可以通过产品名称访问它。
这里给大家做了一个使用示例:
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 个数量
按下前
按下后
你在这里有一个关于如何访问和更新地图的元素或值的概念(双重地图)