我想在嵌套的孩子中向Flutter中的Firebase添加数据



数据样本

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

要将相同的数据写入/news/category1/post1,您需要执行以下操作:

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

作为侧节点,您似乎在嵌套类别和帖子,这违反了Firebase的建议,以避免嵌套数据并保持数据结构的平坦。

与其有一棵树,我建议有:

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"
|    .
|    .

最新更新