【发布时间】:2023-04-02 11:32:01
【问题描述】:
我的应用程序是一种投票应用程序。所以管理员提出一个问题,用户回答这个问题,每个数据都存储在 Firestore 中。在管理页面,管理员可以看到自己创建的问题,当管理员点击问题时,可以看到用户对该问题的回答。
在答案页面上,我试图访问每个用户的 uid,但我无法做到这一点。我可以打印它们,但我不能以某种方式使用它们。我认为我在使用 Firebase 函数时犯了一些错误。
这是我尝试返回包含特定问题的列表的方法示例。但是这个函数不返回任何东西。
List getUserIDData() {
List<String> growableList = [];
List<String> userAnswerList = [];
String adminQuestion = "What is the reason of life?";
Firestore.instance.collection("user").getDocuments().then((snapshot) {
snapshot.documents.forEach((element) {
growableList.add(element.documentID);
});
for (int i = 0; i < growableList.length; i++) {
Firestore.instance
.collection("user")
.document(growableList[i])
.collection("answers")
.where("adminQuestion", isEqualTo: adminQuestion)
.getDocuments()
.then((snapshot) {
snapshot.documents.forEach((element) {
userAnswerList.add(element["userAnswer"]);
});
});
print(i);
}
});
return growableList;
}
我也尝试过使用 StreamBuilder,但在 Firestore.instance 之后,所有内容都会自动删除。在 Firestore 中,一切看起来都不错,但是在完成后,growableList 并没有保持不变,并且此函数返回 null。
你有什么建议吗?
【问题讨论】:
标签: database firebase flutter dart google-cloud-firestore