【问题标题】:get data from all the docs of firebase in flutter从 Flutter 中的所有 firebase 文档中获取数据
【发布时间】:2021-03-01 13:25:47
【问题描述】:

我希望将所有 id 的 indivisualscore 打印在控制台上。

当我搜索每个传递的个人 ID 时,它给了我正确的答案。例如

 CollectionReference collectionReference =  FirebaseFirestore.instance.collection('score');
    await collectionReference.doc(widget.uid).collection("indivisualscore").snapshots().listen((event) {
      print(event.docs.length);
    }

我在这里传递 widget.uid,给我传递的特定 id 的 indivisualscore。

但我现在想在分数集合中获取所有 id 的 indivisualscore。 它必须与分数集合中文档的长度有关,但我无法正确获取它。

谢谢。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    如果要获取集合score 中的id,则必须执行以下操作:

    CollectionReference collectionReference = FirebaseFirestore.instance.collection('score');
    var snapshot = await collectionReference.get();
     snapshot.docs.forEach((result) {
        print(result.id);
      });
    

    引用集合score,然后使用get() 获取该集合中的文档,然后属性docs 将返回一个List<DocumentSnapshot>,您可以对其进行迭代并获取所有ID。

    【讨论】:

    • 你在哪里粘贴的?
    • 如果它没有打印,那么它甚至没有进入它。你必须调试它来检查
    • 但对于独立的 uid,它的打印。我调试了它,它运行你的代码,但没有打印任何东西。
    • 打印打印(snapshot.docs);在控制台中显示空 erray,仅打印 snapshort 会在控制台中提供“QuerySnapshot”实例
    • 感谢@PeterHaddad 指出错误,非常感谢
    猜你喜欢
    • 2021-01-06
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多