【问题标题】:how to send list<dynamic> to flutter firestore如何将列表<动态>发送到颤动的火库
【发布时间】:2021-08-07 03:46:58
【问题描述】:

我将一个列表 (test1) 与另一个列表 (test2) 合并,并获得了动态列表 (marge1),我需要将 marge1 转换为 Map 以将其发送到 Firestore,目前我了解.但我陷入了困境。会有什么解决办法。我的代码如下:

    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。我已经尝试过,但无法弄清楚。任何帮助表示赞赏。

【问题讨论】:

  • 你用过json.encode(YOUR DATA)吗?
  • 是的,我做到了,但是当我尝试在 setData 上使用它时,它显示 list,因为它需要 Map

标签: flutter google-cloud-firestore


【解决方案1】:

marge1List&lt;Map&lt;String, dynamic&gt;&gt;,您需要编写每个 Map&lt;String, dynamic&gt; 项目,而不是将列表本身转换为 Map&lt;String, dynamic&gt;

您需要使用batched write

批量写入:批量写入是对一个或多个写入操作的集合 更多文档。

Source

你可以这样做:

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 documentation

【讨论】:

    猜你喜欢
    • 2020-06-07
    • 2019-05-19
    • 2019-09-15
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    相关资源
    最近更新 更多