'What can be added or remove in this dart class

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



Solution 1:[1]

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

Solution 2:[2]

Make your fields nullable by adding ? to the type.

class Question {

  String? questionText;
  bool? questionAnswer;

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jobin