如何更新Firebase中数组中的地图数据?(扑)

如何更新Firebase中数组中的地图数据?(扑)

问题描述:

我正在使用Flutter Web和Firebase进行项目,但遇到了问题.我正在尝试更新Firestore中数组中的地图.

I'm using flutter web and firebase for a project and got stuck on a problem. I'm trying to update a map in an array in firestore.

使用此:

var val = [];
    val.add({'groupUID': groupUID, 'inviteStatus': status});
    var userInviteUID;
    await users
        .document(uid)
        .get()
        .then((value) => userInviteUID = value.data['inviteUID']);
    await invites
        .document(userInviteUID)
        .updateData({'invites': FieldValue.arrayUnion(val)});

我得到了这个结果: firestore结构

我想做的就是将地图中的1更改为2.我以为它会更新,因为它的值相同,但只是将其添加到数组中.

What I want to do is just change the 1 to a 2 in the map. I thought that it would update since its the same value but it just adds it to the array.

我在堆栈上四处张望,看到了一些方法,例如复制整个数组并在需要的地方更改它,然后将其重新添加.

I looked around on stack and saw some ways to do it like copying the entire array and changing it where I need to, then adding it back.

但是我想知道是否有一种方法可以通过对我的代码进行一些修改来避免这种情况.还请告诉我是否应该使用更好的结构.感谢帮助!

But I wanted to know if there was a way to avoid that by adding some modifications to my code. Also let me know if there's a better structure I should use. Appreciate the help!

更新:

var ref = invites.document(userData.inviteUID);
    ref.get().then((value) async {
      var invitesList = value.data['invites'];

      switch (status) {
        case 1:
          break;
        case 2:
          var index;
          invitesList.asMap().forEach((key, value) {
            if (value['groupUID'] == groupUID) index = key;
          });
          invitesList.removeAt(index);
          await invites
              .document(userData.inviteUID)
              .updateData({'invites': FieldValue.arrayUnion(invitesList)});
          break;
        default:
      }

因此,我查看了一些打印语句,并发现具有匹配组uid的元素已被删除,但查看Firebase,该数组没有被任何东西覆盖...任何想法?

So I looked at some print statements and seen that the elements with the matching group uid is removed, but looking at firebase, the array isn't overwritten anything...any ideas?

最终更新:

var ref = invites.document(userData.inviteUID);
        ref.get().then((value) async {
          var invitesList = value.data['invites'];
    
          switch (status) {
            case 1:
              break;
            case 2:
              var index;
              invitesList.asMap().forEach((key, value) {
                if (value['groupUID'] == groupUID) index = key;
              });
              invitesList.removeAt(index);
              await invites
                  .document(userData.inviteUID)
                  .setData({'invites': FieldValue.arrayUnion(invitesList)});
              break;
            default:
          }

通过将updateData更改为setData来解决.

Fixed it by changing updateData to setData.

我在堆栈上四处张望,看到了一些方法来做到这一点,例如复制整个数组并在需要的地方更改它,然后将其重新添加.

I looked around on stack and saw some ways to do it like copying the entire array and changing it where I need to, then adding it back.

这正是修改Firestore文档中数组内容的方式.Firestore不支持按索引更新数组元素.

That's exactly how are you supposed to modify the contents of arrays in Firestore documents. Firestore doesn't support updating array elements by index.