[英]Flutter Firestore, How to add list of objects in array
如图所示,我需要在 Firestore 中添加对象列表。 我只能用下面的代码添加两个列表
onPressed: () {
_fireStore.collection('notifyseller').document().updateData({
'Customer': userName,
"address": controller.text,
"mobile": mobileNumber,
"Item": FieldValue.arrayUnion([
{
"name": itemName.toList()[0],
"price": rate.toList()[0],
"quantity": quantity.toList()[0]
},
{
"name": itemName.toList()[1],
"price": rate.toList()[1],
"quantity": quantity.toList()[1]
},
]),
});
},
这里 itemName.toList() 包含字符串列表。 通过上面的代码,我只能添加两个数据。 我需要将 itemName.toList() 中的所有项目添加到该数组中,而不是为每个数组提供索引
如果你确定你的三个列表长度相同,试试这个;
List yourItemList = [];
for (int i = 0; i < itemName.length; i++)
yourItemList.add({
"name": itemName.toList()[i],
"price": rate.toList()[i],
"quantity": quantity.toList()[i]
});
_fireStore.collection('notifyseller').document().updateData({
'Customer': userName,
"address": controller.text,
"mobile": mobileNumber,
"Item": FieldValue.arrayUnion(yourItemList),
});
我这样做了
class Item{
String name;
String price;
String qty;
//named constructor here
Map<String, dynamic> toMap() {
return {
'name': name,
'price': price,
'qty':qty,
};
}
}
List<Items> items = [Item(name:"",price:"",qty:""),Item(name:"",price:"",qty:"")]
_fireStore.collection('notifyseller').document().updateData({
'Customer': userName,
"address": controller.text,
"mobile": mobileNumber,
"Item":, items.map<Map>((e)=> e.toMap()).toList();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.