Home > Net >  What can be added or remove in this dart class
What can be added or remove in this dart class

Time:03-08

Please what could be the error in this dart class.

class Question {

  String questionText;
  bool questionAnswer;

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

Error log:

error: Non-nullable instance field 'questionText' must be initialized. (not_initialized_non_nullable_instance_field at [practice] lib\question.dart:5)
error: Non-nullable instance field 'questionAnswer' must be initialized. (not_initialized_non_nullable_instance_field at [practice] lib\question.dart:5)

error image

CodePudding user response:

class Question {
  String QuestionText;
  bool QuestionAnswer;

  Question(this.QuestionText, this.QuestionAnswer);
}

OR

class Question {
  late String QuestionText;
  late bool QuestionAnswer;

  // Question(this.QuestionText, this.QuestionAnswer);
  Question(String q, bool s) {
    QuestionAnswer = s;
    QuestionText = q;
  }
}

CodePudding user response:

Make your fields nullable by adding ? to the type.

class Question {

  String? questionText;
  bool? questionAnswer;

  Question({String q, bool a}) {
    questionText = q;
    questionAnswer = a;
  }
}
  • Related