Ad

Flutter: How To Create Nested Objects In Flutter To Store On Cloud Firestore

I am looking for a way to build a collection of categories and sub categories for a market place in Cloud Firestore, I wan to create an Categories and inside every category Map each category with key value pairs, basically a tree stucture. Something similar to down below

Category 1 ----- > Array

0 map

Field ----> String -----> Value

Field----->String------> Value

1 map

Field ----> String -----> Value

Field----->String------> Value

var results = [
    'Category 1' = [
      {field: string , field: string, field: int}
      {field: string , field: string, field: int}
      {field: string , field: string, field: int}
      'Category x'= [
        {field: string , field: string, field: int}
        {field: string , field: string, field: int}
      ]
    ]
  ];  

This is not a code, this is just the type of structure I am looking for to create in cloud firestore, I am also attaching an image as how to structure will look on cloud firestore.
enter image description here

Ad

Answer

  var result = {
    "result": {
      "category1": [
        {
          "field1": "string",
          "field2": "string",
          "field3": 10
        },
        {
          "field1": "string",
          "field2": "string",
          "field3": 10
        },
        {
          "field1": "string",
          "field2": "string",
          "field3": 10
        },
        {
          "category2": [
            {
              "field1": "string",
              "field2": "string",
              "field3": 10
            },
            {
              "field1": "string",
              "field2": "string",
              "field3": 10
            }
          ]
        }
      ]
    }
  };


//save to firestore
    Firestore.instance.collection(id).document().setData(result);

enter image description here

Ad
source: stackoverflow.com
Ad