How can I update map data that's in a array in firebase? (Flutter)

↘锁芯ラ 提交于 2021-02-08 03:06:28

问题


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.

using this:

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)});

I got this result: firestore structure

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!

UPDATE:

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:
      }

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?

FINAL UPDATE:

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:
          }

Fixed it by changing updateData to setData.


回答1:


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.

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.




回答2:


I had the hope that something would have changed over the last 3 months on Firestore documents, but as it seems, Doug Stevenson's reply is still the only answer.

You need to copy the entire array, change it locally, and after that make the post request on Firebase.



来源:https://stackoverflow.com/questions/62783284/how-can-i-update-map-data-thats-in-a-array-in-firebase-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!