Home > OS >  Non-nullable instance field 'questionAnswer' must be initialized. (Documentation), Non-nul
Non-nullable instance field 'questionAnswer' must be initialized. (Documentation), Non-nul

Time:10-01

enter image description here

I find this code working on previous version of flutter but it's not working on my new updated version. Can Anyone have a solution for this problem? Thank u :)

CodePudding user response:

Use constructor like:

class Question {
  String questionText;
  bool questionAnswer;

  Question({required this.questionText, required this.questionAnswer});
}

or go with:

class Question {
  late String questionName;
  late bool questionAnswer;

  Question({required String q,required bool a}){
    questionName = q;
    questionAnswer = a;
  }
}

The constructor cant have null value and should pass something to it

CodePudding user response:

Replace your string String questionText; to String questionText = "";

class Question {
  String questionText = "";
  bool questionAnswer = "";

  Question({required this.questionText, required this.questionAnswer});
}
  • Related