Home > Software design >  A value of type 'Future<List<Questoes>>' can't be assigned to a variable
A value of type 'Future<List<Questoes>>' can't be assigned to a variable

Time:06-26

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.

CodePudding user response:

Add an await

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

CodePudding user response:

Future<void> bla() async {
  List<Questoes> sampledata = await loadQuestions('6s3k8');
}
  • Related