Flutter Firestore,如何在数组中添加对象列表

Asb*_*yas 4 dart firebase flutter google-cloud-firestore

我需要在 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]
            },
          ]),
        });
      },
Run Code Online (Sandbox Code Playgroud)

这里 itemName.toList() 包含字符串列表。通过上面的代码,我只能添加两个数据。我需要将 itemName.toList() 中的所有项目添加到该数组中,而不是为每个数组提供索引


在此处输入图片说明

mal*_*m91 5

如果您确定您的三个列表具有相同的长度,请尝试此操作;

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),
});
Run Code Online (Sandbox Code Playgroud)