在dart中使用async/wait而不是使用http包进行等待



我已经阅读了之前发布的与该主题相关的问题,但不明白发生了什么。我想我已经完成了与官方飞镖/扑动视频中解释的完全相同的过程-https://www.youtube.com/watch?v=SmTCmDMi4BY

我想做的很简单。我想做一个API调用,需要等到我得到响应。但我看不出该怎么办。也许我对async/wait的理解是错误的。如果我检查控制台输出,我会得到这样的

Staring the Program
Ending the Program
Got the questions list
[Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question']

有人能帮我吗。

import 'package:http/http.dart' as http;
import 'dart:convert';
void main() {
print('Staring the Program');
getQuestions();
print('Ending the Program');
}
void getQuestions() async {
QuizModel qm = QuizModel();
List<Question> questionsList = await qm.getQuestions();
print('Got the questions list');
print(questionsList);
}
class Question {
String question;
bool answer;
Question({this.question, this.answer});
}
class QuizModel {
var quizData;
Future<List<Question>> getQuestions() async {
NetworkHelper networkHelper =
NetworkHelper('https://opentdb.com/api.php?amount=10&type=boolean');
quizData = await networkHelper.getData();
final List<Question> questionsList = [];
for (final question in quizData['results']) {
bool answer;
question['correct_answer'] == 'True' ? answer = true : answer = false;
questionsList
.add(Question(question: question['question'], answer: answer));
}
return questionsList;
}
}
class NetworkHelper {
final String _url;
NetworkHelper(this._url);
Future<dynamic> getData() async {
http.Response response = await http.get(_url);
if (response.statusCode == 200) {
var jsonData = jsonDecode(response.body);
return jsonData;
} else {
print(response.statusCode);
}
}
}

QuizModel.getQuestions()是一个未来。

您的程序正在打印开始,调用未来,打印结束。它不等待未来,getQuestions。

您可以将async添加到main中,并等待方法调用。

您正在打印一个问题列表实例。如果你想打印问题项目,你需要做这个

void getQuestions() async {
QuizModel qm = QuizModel();
List<Question> questionsList = await qm.getQuestions();
print('Got the questions list');
for(final question in questionsList){
print(question.question);
}
}

而不是

void getQuestions() async {
QuizModel qm = QuizModel();
List<Question> questionsList = await qm.getQuestions();
print('Got the questions list');
print(questionsList);
}
I/flutter (18222): Staring the Program
I/flutter (18222): Got the questions list
I/flutter (18222): [Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question']
I/flutter (18222): Ending the Program

如果你需要得到这个打印,你必须更换这个

void main() {
print('Staring the Program');
getQuestions();
print('Ending the Program');
}
void getQuestions() async {
QuizModel qm = QuizModel();
List<Question> questionsList = await qm.getQuestions();
print('Got the questions list');
print(questionsList);
}

用这个

void main()async {
print('Staring the Program');
await getQuestions();
print('Ending the Program');
}
Future<void> getQuestions() async {
QuizModel qm = QuizModel();
List<Question> questionsList = await qm.getQuestions();
print('Got the questions list');
print(questionsList);
}

最新更新