0

I got the following Json data through the drawing-related library.

                            {
                              "bounds": {"width": 525.0, "height": 735.0},
                              "paths": [
                               // draw line 
                                [
                                  {"x": 470.0, "y": 98.0, "t": 1657102762880}
                                  {"x": 470.0, "y": 98.0, "t": 1657102762880}
                                ],
                                [
                                  {"x": 470.0, "y": 98.0, "t": 1657102762880}
                                ]
                              ],
                              "threshold": 0.01,
                              "smoothRatio": 0.65,
                              "velocityRange": 2.0
                            }

And I'm going to upload this data to firestore. (To upload drawing data and load it later)

However, when I upload it to Firestore, the following error is printed

Nested arrays are not supported.

Here's the code I tried

                    await FirebaseFirestore.instance
                            .collection(COL_ROOMS)
                            .doc(widget.roomKey)
                            .collection(COL_DRAW)
                            .doc()
                            .set({
                              "bounds": {"width": 525.0, "height": 735.0},
                              "paths": [
                                [
                                  {"x": 470.0, "y": 98.0, "t": 1657102762880}
                                ]
                              ],
                              "threshold": 0.01,
                              "smoothRatio": 0.65,
                              "velocityRange": 2.0
                            })
                            .then((value) => logger.d("upload"))
                            .catchError((error) => logger.e(error));

Can I know a good way to solve this problem?

Kevin Yang
  • 629
  • 1
  • 5
  • 19
  • 1
    What do you mean by "turning off"? Crashing? Do you have any logs? Have you initialized your app by using `await Firebase.initializeApp()`? – mfkw1 Jul 06 '22 at 13:30
  • When an Android app crashes, it writes an error message and stack trace to its logcat. Please find those, and add them to your question by clicking the `edit` link under it. Also see https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this and https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Frank van Puffelen Jul 06 '22 at 14:08
  • I checked the error message "Nested arrays are not supported." . I'll correct the post – Kevin Yang Jul 07 '22 at 00:43

1 Answers1

2

The problem is in the paths field.

"paths": [
   [
     {"x": 470.0, "y": 98.0, "t": 1657102762880}
   ]
 ]

You have an array of arrays, this is not supported by Firestore.

If you just want to store the data, you can try to store it as a String.

// Covert to string
final dataAsString = json.encode({
    "bounds": {"width": 525.0, "height": 735.0},
    "paths": [
      [
        {"x": 470.0, "y": 98.0, "t": 1657102762880}
      ]
    ],
    "threshold": 0.01,
    "smoothRatio": 0.65,
    "velocityRange": 2.0
  });

// Convert to Map
final dataAsMap = json.decode(dataAsString);
Yayo Arellano
  • 3,575
  • 23
  • 28