I'm trying to read a json from local file and return a list of elements inside, but it's giving this error.
Future<List<Questoes>> loadQuestions(String gameCode) async {
QuestionModel fullQuestions = await getFileContents(gameCode);
List<Questoes> questoes = fullQuestions.questoes!;
print(questoes);
return questoes;
}
List<Questoes> sampledata = loadQuestions('6s3k8');
This is where sampledata gets initialized, and it's inside the quiz controller. The function loadQuestions is after the QuestionModel class (json to dart)
final List<Questoes> _questions = sampledata;
List<Questoes> get questions => _questions;
I have tried using a .then method but it did not work. Thanks for the help.
添加等待
List<Questoes> sampledata = await loadQuestions('6s3k8');
As loadQuestions
is async
, one needs to add an await
when calling it.
Also, if using await
, it has to be placed inside a method using async
itself.
Future<void> bla() async {
List<Questoes> sampledata = await loadQuestions('6s3k8');
}
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.