This is my Question Object:
class Question {
String text;
String correctAnswer;
bool answer;
Question(String q, bool a, String ca) {
text = q;
answer = a;
correctAnswer = ca;
}
}
I want to get a List of Questions from Firestore like so:
List<Question> questionBank = [];
Firestore looks like this: firestore
How can i achieve this?
you can get data from firestore like this
void _onPressed() {
firestoreInstance.collection("Questiions").get().then((querySnapshot) {
querySnapshot.docs.forEach((result) {
print(result.data());
});
});
}
For further details you can refer this https://medium.com/firebase-tips-tricks/how-to-use-cloud-firestore-in-flutter-9ea80593ca40
Retrieve questions from Cloud Firestore and convert to list:
Future<List<Question>> fetchQuestions(String userId) async {
final questions = new List<Question>();
final doc = await FirebaseFirestore.instance.collection('Questions').doc(userId).get();
final questionsTmp = doc.data().questions;
questionsTmp.forEach((questionTmp) {
questions.add(Question.fromMap(questionTmp));
});
return questions;
}
Add fromMap method to Question class:
class Question {
String text;
String correctAnswer;
bool answer;
Question(String q, bool a, String ca) {
text = q;
answer = a;
correctAnswer = ca;
}
static Question fromMap(Map<String, dynamic> map) {
return Question(
map['text'],
map['answer'],
map['correctAnswer'].ToString() == 'true'
);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.