gpt4 book ai didi

android - 我想在Flutter中将数据添加到Firebase中的嵌套子级中?

转载 作者:行者123 更新时间:2023-12-03 03:31:29 25 4
gpt4 key购买 nike

数据样本

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

关于android - 我想在Flutter中将数据添加到Firebase中的嵌套子级中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63672137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com