【问题标题】:I want to add data to Firebase in Flutter in nested child?我想在嵌套子项中将数据添加到 Flutter 中的 Firebase?
【发布时间】:2020-12-19 15:38:27
【问题描述】:

数据样本

database
|__news
    |__category1
    |    |
    |    |__post1
    |    |    |__text: "Lorem ipsum 1"
    |    |    |__title: "Title of post1"
    |    |__post2
    |    |    |__text: "Lorem ipsum 2"
    |    |    |__title: "Title of post2"
    |    |__description: "description text"
    |    |__id: "id of category"
    |    .
    |    .
    |
    |__category2
    |    |
    |    |__post34
    |    |    |__text: "Lorem ipsum 34"
    |    |    |__title: "Title of post34"
    |    .
    |    .

现在我有一个在 Firebase 中创建数据的简单函数是:

 void createRecord() {
    databaseReference.child("post1").set({
      'text': 'Mastering EJB',
      'title': 'Programming Guide for J2EE'
    });

我的问题是我想在=>news->category1>[posts] 添加数据 那么我应该怎么做才能迭代到post1

【问题讨论】:

    标签: android firebase flutter dart firebase-realtime-database


    【解决方案1】:

    要将相同的数据写入/news/category1/post1,您可以:

    databaseReference.child("/news/category1/post1").set({
      'text': 'Mastering EJB',
      'title': 'Programming Guide for J2EE'
    });
    

    作为一个侧节点,您似乎在嵌套类别和帖子,这违反了 Firebase 对 avoid nesting datakeeping data structures flat 的建议。

    我建议不要拥有一棵树,而是:

    database
    |__news
        |__category1
        |    |
        |    |__post1
        |    |    |__text: "Lorem ipsum 1"
        |    |    |__title: "Title of post1"
        |    |__post2
        |    |    |__text: "Lorem ipsum 2"
        |    |    |__title: "Title of post2"
        |    .
        |    .
        |
        |__category2
        |    |
        |    |__post34
        |    |    |__text: "Lorem ipsum 34"
        |    |    |__title: "Title of post34"
        |    .
        |    .
    |__news_categories
        |__category1
        |    |__description: "description text"
        |    |__id: "id of category"
        |
        |__category2
        |    |__description: "description text of category 2"
        |    |__id: "id of category2"
        |    .
        |    .
    

    【讨论】:

      猜你喜欢
      • 2018-08-18
      • 1970-01-01
      • 2020-09-07
      • 2019-07-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2022-08-04
      • 2022-12-01
      相关资源
      最近更新 更多