skip to Main Content

What I’ve been trying to attempt is to update the values of an array that is concatenated within a map. Here is the data structure:

enter image description here

As you can observe, I want to add a new map within the "tasks" field. A map containing the same data as the current map the "tasks" field has. This is my approach:

                    int index = list
                        .indexWhere((element) => element.containsKey('tasks'));

                    // Update the 'tasks' field of the element at the found index
                    transaction
                        .update(catalogueCollection.doc(widget.llistId), {
                      'list.$index.tasks': FieldValue.arrayUnion([
                        {
                          'correct': true,
                          'value': '',
                        },
                      ]),
                    });
                  }
                });
              },

What this does is somehow overwrite my entire list array (I think?) and make my list into a map even tho it was a list beforehand. It overall makes me confused and would like a way to only update the tasks field so that I can add new maps in them. Appreciate it. This is my first question in Stackoverflow so I hope I’m being clear with my problem 🙂

2

Answers


  1. This is an array inside a map inside another array. As far as my knowledge goes, this kind of operation is beyond typical array operations and must be handled in a transaction or a dedicated arrayUnion on the top level with the new version and an arrayRemove on the old entry. this is because Firebase, while great, has limited tools and does not support any sort of index thumbprint query operation.

    at that point it would be an SQLite style database.

    Login or Signup to reply.
  2. You cannot use arrayUnion to update an array that exists inside a map, which in terms exists in another array. In such cases, the only option that you have is to read the document, perform the necessary updates in memory and then write the document back to Firestore.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search