简体   繁体   English

我想从 firestore 中的集合中获取多个字段的文档

[英]I want to get documents for multiple fields from a collection in firestore

"titleList" is a list of names of collection documents. “titleList”是集合文档的名称列表。 For "questionMaps", the document name is the key and the "text" field of the document's array type is the value.对于“questionMaps”,文档名称是键,文档数组类型的“文本”字段是值。 I would like to make this questionMaps have 30 elements, but I have tried using a for statement but it did not work.我想让这个 questionMaps 有 30 个元素,但我尝试使用 for 语句但它没有用。 I would like to know how to solve this problem.我想知道如何解决这个问题。 This is my code.这是我的代码。

Map questionMaps = {};
for (int i = 0; i < 30; i++) {
   FirebaseFirestore.instance
      .collection("titles")
      .doc(widget.titleList[i])
      .get()
      .then(
      (ref) {
         List questions = ref.get("text");
         questionMaps[widget.titleList[i]] = questions;
        },
       );
      }

consider trying to get the whole snapshot of the collection, then run your logic over it.考虑尝试获取集合的整个快照,然后对其运行逻辑。 since you said that your documents ids are the names in your titleList list, consider this:既然你说你的文档 ID 是你的titleList列表中的名字,考虑一下:

   Map questionMaps = {};

   final querySnapshot = await FirebaseFirestore.instance.collection("titles").get();

   querySnapshot.docs.forEach((doc) {
     List questions = (doc.data() as Map<String, dynamic>)["text"];
     questionMaps[doc.id] = questions; 
   });

this will register your questions List for each doc.id , then you can get it based on titleList like this as example:这将为每个doc.id注册您的问题List ,然后您可以基于titleList获取它,例如:

print(questionMaps[titleList[0]]); // will get you questions which have the  name key.

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

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