[英]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.