Flutter Firestore - update data with List of List of Array

2,153

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.

Share:
2,153
L.Keysoft
Author by

L.Keysoft

Updated on December 25, 2022

Comments

  • L.Keysoft
    L.Keysoft over 1 year

    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 ?

    • Cgrain
      Cgrain over 3 years
      Could you add the error log?
    • Ralemos
      Ralemos over 3 years
      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 on the benefits of a subcollections approach, if this is possible for your scope.