sulli110
sulli110

Reputation: 347

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

Upvotes: 0

Views: 1430

Answers (1)

Junsu Cho
Junsu Cho

Reputation: 854

  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

Upvotes: 1

Related Questions