【问题标题】:Flutter: How to create nested objects in Flutter to store on Cloud FirestoreFlutter:如何在 Flutter 中创建嵌套对象以存储在 Cloud Firestore
【发布时间】:2020-11-27 07:11:24
【问题描述】:

我正在寻找一种方法来为 Cloud Firestore 中的市场构建类别和子类别的集合,我想创建一个类别并在每个类别中使用键值对映射每个类别,基本上是一个树结构。类似于下面的东西

类别 1 -----> 数组

0 地图

字段 ----> 字符串 -----> 值

字段----->字符串----->值

1 张地图

字段 ----> 字符串 -----> 值

字段----->字符串----->值

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}
      ]
    ]
  ];  

这不是代码,这只是我要在 Cloud Firestore 中创建的结构类型,我还附上了一张图片,说明如何在 Cloud Firestore 上查看结构。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore nested-lists


    【解决方案1】:
      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);
    

    【讨论】:

    • 谢谢你的回答,但是这是手动完成的,我有两个文本字段小部件,我想通过它们在类别中添加字段并且 ID 是自动生成的,我将如何做到这一点,如何使用这些 TextField 小部件动态添加。
    • Firestore.instance.collection(textField1.value.text).document(textField2.value.text).setData(result);
    猜你喜欢
    • 2021-05-25
    • 2022-01-03
    • 1970-01-01
    • 2021-09-30
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    相关资源
    最近更新 更多