1

I want to update data in Firestore with Flutter. My data is List<List<Map<String,dynamic>>>

This works (Map in Array):

await _firestore
          .collection("postsByPostId")
          .doc(postId)
          .update(
            {
              "imageTags": [
                {
                  "1": "this works",
                },
                {
                  "2": "this works",
                },
              ],
            },
          )
          .then((value) => print("Post Updated"))
          .catchError(
            (error) => print("Failed to update post: $error"),
          );

but this makes the app crash : (Map in Array in Array)

await _firestore
          .collection("postsByPostId")
          .doc(postId)
          .update(
            {
              "imageTags": [
                [
                  {
                    "1": "this doesn't works",
                  },
                ],
                [
                  {
                    "2": "this doesn't works",
                  },
                ],
              ],
            },
          )
          .then((value) => print("Post Updated"))
          .catchError(
            (error) => print("Failed to update post: $error"),
          );

My goal is too update data with :

List<List<Map<String,dynamic>>> postReadyListOfTag = [
                            [
                              {
                                "positionOrientation": "topLeft",
                                "text": "text",
                                "markUpText": "markUpText",
                                "topPositionedPercentage": 10.0,
                                "bottomPositionedPercentage": 5.0,
                                "rightPositionedPercentage": 5.0,
                                "leftPositionedPercentage": 10.0,
                              },
                            ],
                            [
                              {
                                "positionOrientation": "topLeft",
                                "text": "text",
                                "markUpText": "markUpText",
                                "topPositionedPercentage": 10.0,
                                "bottomPositionedPercentage": 5.0,
                                "rightPositionedPercentage": 5.0,
                                "leftPositionedPercentage": 10.0,
                              },
                            ],
                          ];

How can I please achieve that ?

L.Keysoft
  • 308
  • 1
  • 10
  • Could you add the error log? – Cgrain Nov 12 '20 at 09:54
  • Please share the error message so we can check what is going on. Also, do you really need this approach of object inside an array of arrays? An approach of subcollections would be better for a number of reasons, for example you could filter queries more easily and accurately, it would be easier to maintain the data and less error prone. Check this [community question](https://stackoverflow.com/questions/54266090) on the benefits of a subcollections approach, if this is possible for your scope. – Ralemos Nov 12 '20 at 12:48

2 Answers2

1

As explained in the comments, the List<List<Map<String,dynamic>>> approach is not ideal for a number of reasons:

  • Firestore has a document limit of 1mb of size, which could be a problem when you begin to nest a big number of objects inside multiple layers of arrays.

  • You cannot segregate rules in the Firebase Rules with this approach, since you can't block only part of a document.

  • Firestore has a document field count limit of 20k fields, and maps fields are part of that count, this is a problem since Firestore creates indexes for that and could be a performance issue for your app in the future.

A better approach would be to use subcollections which would mitigate all those issues since you could filter queries more easily and accurately, it would also be easier to maintain the data, more secure and less error prone.

You can find more details in this Get to know Cloud Firestore video and in this community question on the benefits of a subcollections approach.

Ralemos
  • 5,571
  • 2
  • 9
  • 18
-2

You are right, this List<List<Map<String,dynamic>>> approach doesn't fit the Firebase behavior. I will change to List & sub-collections. Thank you for your help guys.

L.Keysoft
  • 308
  • 1
  • 10