简体   繁体   English

Firebase Firestore get all Docs 在 Flutter 中为空

[英]Firebase Firestore get all Docs is empty in Flutter

I want to get all sub docs and collections from the collection.我想从集合中获取所有子文档和 collections。

Database structure:数据库结构:

Code Snippet:代码片段:

_firestore.collection("messages").get().then((querySnapshot) => {getdata(querySnapshot)});

But querySnapshot.docs.length is always zero.querySnapshot.docs.length始终为零。 How can I try to get all data from collection?我怎样才能尝试从集合中获取所有数据?

Hrmmm what does your current doc query look like? Hrmmm 您当前的文档查询是什么样的?

The id of your documents are a bit confusing, but I'd assume the standard query should work.您的文档的 ID 有点令人困惑,但我认为标准查询应该有效。

NOTE : Using sequential keys/numbers as document and collections ID's is dangerous and will give you a lot of problems later down the line.注意:使用顺序键/数字作为文档和 collections ID 是危险的,以后会给您带来很多问题。 If you're not generating unique-ids yourself, let Firestore do it for you.如果您不自己生成唯一 ID,请让 Firestore 为您生成。 Firebase - best practices Firebase - 最佳实践

Eg例如

To listen to the changes of the messages/2/1 collection:监听messages/2/1集合的变化:

 final snapshotStream = FirebaseFirestore.instance
        .collection('messages/2/1')
        .snapshots()
        .asBroadcastStream();

This returns a stream which contains a snapshot of each of the documents in there that will be updated in 'real-time'.这将返回一个 stream,其中包含将“实时”更新的每个文档的快照。 The .asBroadCastStream() ensures that you can register multiple listeners to that stream. (Looks like you're using this as some sort of chat feature?) .asBroadCastStream()确保您可以向该 stream 注册多个侦听器。(看起来您将其用作某种聊天功能?)

A little extra, one way to access all the documents within the stream:一点额外的,一种访问 stream 中所有文档的方法:

 await for (final streamSnapshot in snapshotStream) {
          final documentsList = streamSnapshot.docs;
          /// Do something with documentsList
 }

Let me know how it goes, good luck: :)让我知道进展如何,祝你好运::)

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

相关问题 Flutter & firebase 通过 geohash 获取文档 - Flutter & firebase get docs by geohash Flutter Firestore - 仅获取 10 个带有 ID 的文档 - Flutter Firestore - get only 10 docs with their id Flutter/Firestore - 查询快照不包括所有文档 - Flutter/Firestore - query snapshot does not include all docs 如何在 Firestore 集合组查询 Flutter 中获取子集合文档 - How to Get Subcollection docs in Firestore Collection Group Query Flutter 如何更新 flutter 中 firebase 火库中的所有集合 - How to update all collection in firebase firestore in flutter Angular Firestore Group collection 获取多个文档下的所有文档 - Angular Firestore Group collection get all docs under a multiple document 如何在 Firestore 中一次获取一组文档 ID 的所有文档,但只获取这些文档中的部分属性,而不是整个文档? - How to get all documents at once for a set of doc IDs, but only part of the properties in those docs, not the entire docs, in Firestore? 从 Firestore 子集合 flutter 获取所有数据 - get all data from a firestore subcollection flutter Flutter web Firebase Firestore - Flutter web Firebase Firestore Firebase Firestore 安全规则 - 根据资源读取所有嵌套文档(但它为空) - Firebase Firestore security rules - read all nested docs based on resource (but it's null)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM