【发布时间】:2021-12-27 20:35:06
【问题描述】:
我目前正在开发 Flutter 中的测验应用程序。我正在从 firebase 获取数据并成功创建了包含我所需数据的地图列表。
但问题是我在 snapshot.docs 上执行 forEach 循环并将值添加到空的 AllQuestionsOfQuiz 列表中,但我无法从函数中返回它。
我不知道我在这里缺少的是代码:
import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:kbc/screen/question.dart';
class QuizCreator {
static List AllQuestionsOfQuiz = [];
static Future<List<String>> fetchOptions(
QueryDocumentSnapshot<Map<String, dynamic>> questionSnap) async {
List<String> queOptions = [];
await questionSnap.reference
.collection("options")
.get()
.then((value) async {
value.docs.forEach((element) {
queOptions.add(element.data()["opt1"]);
queOptions.add(element.data()["opt2"]);
queOptions.add(element.data()["opt3"]);
queOptions.add(element.data()["opt4"]);
});
});
return queOptions;
}
static Future<Map> fetchQuestion(
QueryDocumentSnapshot<Map<String, dynamic>> questionSnap,
String QuePrize) async {
Map questionNOpt = {};
String question;
await questionSnap.reference
.collection("questions")
.get()
.then((value) async {
question = value.docs.elementAt(0)["question"];
questionNOpt["prize"] = QuePrize;
questionNOpt["question"] = question;
questionNOpt["options"] = await fetchOptions(value.docs.elementAt(0));
});
return questionNOpt;
}
static Future<List> quizCreator(String quizID) async {
List QuestionOfQuizzes = [];
await FirebaseFirestore.instance
.collection("quizzes")
.doc(quizID)
.collection("prizes")
.orderBy("prize", descending: false)
.snapshots()
.forEach((snapshot) {
snapshot.docs.forEach((element) async {
await fetchQuestion(element, element.data()["prize"].toString())
.then((value)async {
AllQuestionsOfQuiz.add(value);
});
print(AllQuestionsOfQuiz);
});
});
return AllQuestionsOfQuiz;
}
}
通过这段代码,我在终端中得到以下输出:
I/flutter ( 8913): [{prize: 20000, question: How many workers was there to create the TAL MAHAL?, options: [60000, 70888, 75600, 789000]}]
I/flutter ( 8913): [{prize: 20000, question: How many workers was there to create the TAL MAHAL?, options: [60000, 70888, 75600, 789000]}, {prize: 10000, question: Who was awarded by the Padma Shri In The Field Of Physics ?, options: [Tulasi Gowda, Sujoy K. Guha, Harekala Hajabba, HC Verma]}]
I/flutter ( 8913): [{prize: 20000, question: How many workers was there to create the TAL MAHAL?, options: [60000, 70888, 75600, 789000]}, {prize: 10000, question: Who was awarded by the Padma Shri In The Field Of Physics ?, options: [Tulasi Gowda, Sujoy K. Guha, Harekala Hajabba, HC Verma]}, {prize: 5000, question: Who is Jethalal in TMOCK ?, options: [Husband of daya, Param Mitra Of Mehta, Son of bapuji, Father of tapu]}]
我希望在我的函数quizCreator 中返回最后一次迭代值:
[{prize: 20000, question: How many workers was there to create the TAL MAHAL?, options: [60000, 70888, 75600, 789000]}, {prize: 10000, question: Who was awarded by the Padma Shri In The Field Of Physics ?, options: [Tulasi Gowda, Sujoy K. Guha, Harekala Hajabba, HC Verma]}, {prize: 5000, question: Who is Jethalal in TMOCK ?, options: [Husband of daya, Param Mitra Of Mehta, Son of bapuji, Father of tapu]}]
请帮帮我,我在过去 2 天都被这个错误困住了。
【问题讨论】:
-
Snapshots 返回一个 Stream,其值在加载数据时流入。使用
get而不是snapshots应该只执行一次。
标签: firebase flutter google-cloud-firestore async-await return-value