使用Flutter Firestore插件,对文档中的每个子集合进行操作 [英] Using Flutter Firestore plug in, do something for each sub collection in a document

查看:82
本文介绍了使用Flutter Firestore插件,对文档中的每个子集合进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了帮助我更好地理解Flutter和Firebase,我制作了一个列表共享应用。我正在使用列表主屏幕,该屏幕将显示一个可重新排序的列表图块视图,其中包含每个用户列表的图块,但我尚未开始研究这些列表中的内容。我已经设置了Firestore,以便每个列表都是一个子集合,现在我想为该用户文档中的每个子集合(该用户列表的列表)创建一个磁贴。我很难告诉flutter在不使用特定子收藏集名称的情况下为每个子收藏集执行某项操作。我想通过为每个子collection(list)设置一个collection引用并在每个瓷砖上调用.id来向每个tile传递列表标题。使用集合ID作为标题,我不确定是否可以做到这一点,还是不确定是否必须使列表标题成为每个列表中的一个字段。无论哪种方式,我都需要找出如何为特定文档中的每个子集合做些事情。 .forEach似乎仅适用于文档字段,而不适用于子集合?我究竟做错了什么?我敢肯定,有更好的方法可以解决此问题。我没有包含任何代码,因为这是一个大问题。

To help me better understand Flutter and Firebase, I'm making a list sharing app. I'm working on the list home screen that will show a reorderable list tile view with a tile for each of the users' lists, I have not began to work on whats inside of these lists. I have firestore set up so that each list is a sub collection, now I want to create a tile for each sub collection in that user's document ( a list of that user's lists). I'm having a tough time telling flutter to do something for each sub collection without using a specific sub collection's name. I wanted to pass in a list title to each tile by making a collection reference for each sub collection( list) and calling .id on each one. Using the collection ID as the title, I'm not yet sure if I can do this or if ill have to make the list title a field inside of each list. Either way, I need to find out how to do something for each subcollection inside a particular document. .forEach seems to only work on document fields, not subcollections? What am I doing wrong? I'm sure there is a better way to go about this. I have not included any code as this is a big picture kind of question.

推荐答案

Firestore客户端中没有方法,边的SDK来获取所有集合(或特定文档下的子集合)。这样的API 确实存在于服务器端SDK中,但并不存在于客户端SDK中。

There is no method in the Firestore client-side SDKs to get all collections (or subcollections under a specific document). Such API does exist in the server-side SDKs, but not in the client-side SDKs.

请参阅:

  • Fetching all collections in Firestore
  • How to get all of the collection ids from document on Firestore?

因此,您将通常需要通过更改数据模型来了解集合。例如,通过为每个列表的元数据创建一个文档,然后将列表存储在该文档下具有已知名称的子集合中。这样,您可以通过查询文档来获取所有列表,这在API中是可能的。

So you'll need to know the collections already, typically by changing your data model. For example by creating a document for each list's metadata, and then storing the list items in a subcollection with a known name under that document. That way you can get all lists by querying for the documents, which is possible within the API.

这篇关于使用Flutter Firestore插件,对文档中的每个子集合进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆