简体   繁体   English

Flutter firestore 保存 id 到列表

[英]Flutter firestore save id to list

how can I save all document Ids From firestore inside a list?如何将来自 firestore 的所有文档 ID 保存在列表中? Thats what I tried but I couldn't manage to only save the ID:这就是我尝试过的,但我无法只保存 ID:

    List ticketIds = [];
    
    getTicketIds() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      ticketIds = await FirebaseFirestore.instance
          .collection("users")
          .doc(prefs.getString("userId"))
          .collection("tickets")
          .get()
          .then((val) => val.docs);

Hello you need to loop trough the docs and you can retreive the docs id, here is the code:您好,您需要遍历文档,您可以检索文档 ID,这是代码:

 Future<List<String>> getTicketIds() async {
   SharedPreferences prefs = await SharedPreferences.getInstance();
   List<String> ticketIds = await FirebaseFirestore.instance
     .collection("users")
     .doc(prefs.getString("userId"))
     .collection("tickets")
     .get()
     .then((val) {
      List<String> idOfDocuments = [];
      val.docs.forEach((element) {
         idOfDocuments.add(element.id);
      });

      return idOfDocuments;
  });

  return ticketIds;
 }

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

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