首页 > 解决方案 > 扑。创建列表firestore 文档和集合

问题描述

我正在尝试从 Firestore 获取文档列表(documentID)并将其添加到列表中。我已经看到了一些选择,但由于我在这个领域有点新,也许显而易见的事情对我来说变得困难了。而且我不知道这些行应该在代码中的确切位置,例如在 initState 中。

这是我选择的选项之一,但它只生成实例而不是文档的名称。

final QuerySnapshot result =
      await Firestore.instance.collection('submits').getDocuments();
final List<DocumentSnapshot> documents = result.documents;

List<String> myListString = []; // My list I want to create.
myListString.add(documents);    // The way I try to add the doc list to my String list.

以数据库为例。我想将文档 ID 列表获取到 List-String-

在此处输入图像描述

如果可能的话,你可以告诉我是否有类似的方法来应用它来获得一个 List 但在两个或多个集合的情况下。

标签: flutterdartgoogle-cloud-firestore

解决方案


看起来您想要的是文档ID列表,对吗?

如果是这样,这应该工作:

final QuerySnapshot result =
      await Firestore.instance.collection('submits').getDocuments();
final List<DocumentSnapshot> documents = result.documents;

List<String> myListString = []; // My list I want to create.

documents.forEach((snapshot) {
   myListString.add(snapshot.documentID)
});

推荐阅读