I have a LateInitializationError
in Flutter and I dont know how to fix it. I am making a quiz app and requesting questions from an API. The error only shows up for half a second and afterwards the question loads in. I tried delaying the functions that come afterwards, but that didnt help.
The concrete Error Im getting is:
The following LateError was thrown building Quiz(dirty, state: _QuizState#74551):
LateInitializationError: Field 'currentQuestion' has not been initialized.
When the exception was thrown, this was the stack:
#0 _QuizState.currentQuestion (package:demoapp/main.dart)
#1 _QuizState.build (package:demoapp/main.dart:267:21)
Heres the code:
class Quiz extends StatefulWidget {
@override
State<Quiz> createState() => _QuizState();
}
class _QuizState extends State<Quiz> {
late Questions currentQuestion;
late List<String> answers;
List<bool?> answerValidation = [null, null, null, null];
@override
void initState() {
asyncQuestion();
super.initState();
}
void asyncQuestion() async{
Questions currentQuestion1 = await loadQuestion();
setState((){
currentQuestion = currentQuestion1;
answers = getRandomQuestionList(currentQuestion.wrongAnswers, currentQuestion.correctAnswer);
});
}
validateAndShowQuestion(int userAnswerIndex) {
int correctIndex = getCorrectAnswerIndex(currentQuestion.wrongAnswers, currentQuestion.correctAnswer);
setState(() {
if(userAnswerIndex == correctIndex) {
answerValidation[correctIndex] = true;
AssetsAudioPlayer audioPlayer = AssetsAudioPlayer();
audioPlayer.open(
Audio(
'assets/correct-6033.mp3',
),
);
}
else {
answerValidation[userAnswerIndex] = false;
AssetsAudioPlayer audioPlayer = AssetsAudioPlayer();
audioPlayer.open(
Audio(
'assets/wrong-47985.mp3',
),
);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Spacer(),
Text((currentQuestion.question), style: TextStyle(color: Colors.black, fontSize: 22, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
const Spacer(),
GestureDetector(child: answerCard((answers[0]), context, answerValidation[0]),
onTap: () {
validateAndShowQuestion(0);
},),
GestureDetector(child: answerCard((answers[1]), context, answerValidation[1]),
onTap: () {
validateAndShowQuestion(1);
},),
GestureDetector(child: answerCard((answers[2]), context, answerValidation[2]),
onTap: () {
validateAndShowQuestion(2);
},),
GestureDetector(child: answerCard((answers[3]), context, answerValidation[3]),
onTap: () {
validateAndShowQuestion(3);
},),
const Spacer(),
]
),
),
)
);
}
}
I would really appreciate any kind of help.
I tried delaying the function that comes next, but that didnt work. I also tried to change my variables to nullable variables. The problem with that method is, that the question and answers are being initialized with wrong values.
The problem lies in the following section: Text((currentQuestion.question)
. This code is located within the build method, which runs first. However, at that point, you have not assigned a value to the currentQuestion
variable yet. To resolve this issue, please utilize a loader to wait until the data is loaded. By doing so, you will effectively address the problem.