简体   繁体   English

如何使用 Z047B10EEE763AFB6164E077CF1CC268Z 将文档子集合与文档 JSON map 一起使用 Z035489FF8D0924748149

[英]How can I pass the document sub-collections along with the document JSON map in Flutter using Firebase?

I'm trying to get all the documents in the "businesses" collection from Firebase together with their sub-collections.我正在尝试从 Firebase 及其子集合中获取“企业”集合中的所有文档。

我的 Firestore 收藏的图片

The problem is when I do the query to Firebase like this:问题是当我像这样对 Firebase 进行查询时:

Stream<List<Business>> getBusinesses() {
return _db.collection('businesses').snapshots().map((snapshot) => snapshot
    .docs
    .map((document) => Business.fromJson(document.data()))
    .toList());

} }

, the sub-collections aren't passed with the JSON object document.data() , so in my code, the Business object isn't fully completed, which means there are empty fields (Appointments, ServiceProviders, Services), instead of getting the data from the sub-collections. ,子集合没有通过 JSON object document.data()传递,所以在我的代码中,业务 object 没有完全完成,这意味着服务是空字段(没有完全完成)来自子集合的数据。

So hopefully I've explained the problem well, my question is how can I fetch all the document data including its sub-collections, and parse it to a Business Object?所以希望我已经很好地解释了这个问题,我的问题是如何获取所有文档数据,包括其子集合,并将其解析为 Business Object?

Thanks.谢谢。

What seems to be "the problem" is actually the point of Firestore: Keeping documents shallow so you can only get the data you need.似乎是“问题”实际上是 Firestore 的重点:保持文档浅,因此您只能获取所需的数据。 It's then up to you to structure your data the way it will likely be used in the future.然后,您可以按照未来可能使用的方式来构建数据。

Mind you, subcollections are not fields.请注意,子集合不是字段。

What you can do here, is add a query that fetches the documents in the subcollections (Appointments, ServiceProviders, Services), for each business.您可以在此处添加一个查询,该查询为每个业务获取子集合(约会、服务提供者、服务)中的文档。 You would get the business document Id to use for the query.您将获得用于查询的业务文档 ID。

It would typically look something like:它通常看起来像:

_db.collection('businesses').document(documentId).collection('Appointments')

Mind you, this is potentially too much data.请注意,这可能是太多的数据。 It might be better to fetch the docs in those subcollections only when needed/requested by the user.仅在用户需要/请求时才获取这些子集合中的文档可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM