0

I'm learning flutter by following an online course. I'm at the lesson of creating classes. So we have to create a class that has a string and a bool property. Below is this class:

class Question {
  String questionText;
  bool questionAnswer;

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

And the error is showing like this:

Non-nullable instance field 'questionText' must be initialized.

Non-nullable instance field 'questionAnswer' must be initialized.

Someone please help

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
Sherbo
  • 1

1 Answers1

0

As per the new null-safety rules, you can't initialise a variable of any datatype without specifying its value or doing a null check. Try this:

class Question {
  String? questionText;
  bool? questionAnswer;

  Question({@required String q,@required bool a}) {
    questionText = q;
    questionAnswer = a;
  }
}
Arijeet
  • 935
  • 5
  • 19