扑。创建Firestore文档和集合的List <String>

时间:2020-08-28 15:05:52

标签: flutter dart google-cloud-firestore

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

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

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-87-0d451ee34800> in <module>
      3 for dt in my_list:
      4     for k in order_dict:
----> 5         new_dt = my_list[k]

TypeError: list indices must be integers or slices, not str

示例数据库。我想将文档ID的列表发送到List-String-

enter image description here

如果可能的话,您可以告诉我是否有类似的方法可以应用它来获取列表,但是有两个或更多集合的情况。

1 个答案:

答案 0 :(得分:1)

您似乎想要的是文档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)
});