Home > Software design >  Class member fields not initialized when constructor is called in flutter
Class member fields not initialized when constructor is called in flutter

Time:10-13

I am new to flutter. As a learning project i am building a quiz app. I am using Flutter riverpod for state management. Currently, there are three layers (model, repository, view) in my project. quiz.model.dart is connected to quiz.repository.dart and quiz.repository.dart is connected to home.view.dart. Whenever i call quiz repository it gives me error

LateError (LateInitializationError: Field 'question' has not been initialized.)

The link to the project repository is here. I have also created a new branch error_fix for this purpose. Kindly propose your changes in that branch. Any help will be appreciated as i have already spend a large amount of time on this. Thank You!

CodePudding user response:

You haven't defined the 'this' keyword in the 'Question' class constructor. You should change the code as in the examples below.

change

  Question({
    required category,
    required type,
    required difficulty,
    required question,
    required correctAnswer,
    required answers,
  });

to

  Question({
    required this.category,
    required this.type,
    required this.difficulty,
    required this.question,
    required this.correctAnswer,
    required this.answers,
  });
  • Related