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.


Solution 1: Kaushik Chandru

Add an await

List<Questoes> sampledata = await loadQuestions('6s3k8');


Solution 2: Dabbel

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');
}