如何将列表发送到<dynamic>颤石火库



我有一个列表(test1)与其他列表(test2)和列表动态(marge1),我需要将marge1转换为Map<String,>送到消防局,据我所知。但我被困住了。解决方案是什么?我的代码如下:

margeFunction() {
List test1 = [
{
"id": 0,
"name": customerName,
"address": customerAddress,
"mobile": customerMobile,
"deliveryDate": selectedDate1,
},
];

test1.forEach((element) {
test2.forEach((e) {
if (e["id"] == element["id"]) {
marge1.add(getmarge1(
e["id"],
e["itemName"],
e["description"],
e["itemPrice"],
e["image"],
e["unit"],
e["selectedServiceInList"],
e["uniqueListItem"],
e["subTotalListItem"],
e["counter"],
element["name"],
element["address"],
element["mobile"],
element["deliveryDate"],
));
}
});
});
}
Map<String, dynamic> getmarge1(
int id,
String itemName,
String description,
String itemPrice,
String image,
String unit,
String selectedServiceInList,
String uniqueListItem,
int subTotalListItem,
int counter,
String name,
String address,
String mobile,
DateTime deliveryDate) {
final Map<String, dynamic> data = new Map<String, dynamic>();
data["id"] = id;
data["itemName"] = itemName.toString();
data["description"] = description.toString();
data["itemPrice"] = itemPrice.toString();
data["image"] = image.toString();
data["unit"] = unit.toString();
data["selectedServiceInList"] = selectedServiceInList.toString();
data["uniqueListItem"] = uniqueListItem.toString();
data["subTotalListItem"] = subTotalListItem.toString();
data["counter"] = counter.toString();
data["name"] = name.toString();
data["address"] = address.toString();
data["mobile"] = mobile.toString();
data["deliveryDate"] = deliveryDate.toString();
return data;
}

现在我需要将merge1列表发送到firestore。我已经试过了,但还是想不出来。

marge1List<Map<String, dynamic>>,您将希望编写每个Map<String, dynamic>项,而不是将列表本身转换为Map<String, dynamic>

您需要使用批处理写入。

Batched Writes:批处理写是对一个或多个对象进行的一组写操作更多文档。

来源

你可以这样做:

final FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
final WriteBatch writeBatch = firebaseFirestore.batch();
final CollectionReference collectionReference = firebaseFirestore.collection("collectionPath");
marge1.forEach((element) {
final DocumentReference documentReference = collectionReference.doc();
writeBatch.set(documentReference, element);
});
writeBatch.commit();

签出FlutterFire的BatchedWrite文档

相关内容

最新更新