londonappbrewery/quizzler-flutter-challenge-final

ScoreKeeper Wrong Count

Closed this issue · 3 comments

When we give the answers, it works fine until the last question comes up.
when we select the last question's answer, It does not get added to the scorekeeper and the value of scorekeeper will be always less than the actual score.

@chintanrparmar I discovered that issue when I tried to display the results in the Alert dialog as well. To fix this, you would need to move the part where it checks for the correct answer in the checkAnswer(bool userPickedAnswer) method up to the beginning of the block.
Here's the fixed version of the checkAnswer method:

void checkAnswer(bool userPickedAnswer) {
    bool correctAnswer = quizBrain.getCorrectAnswer();

    setState(() {
      // This has to be done first so it populates the last question before it checks if it finishes
      if (userPickedAnswer == correctAnswer) {
        scoreKeeper.add(Icon(Icons.check, color: Colors.green));
      } else {
        scoreKeeper.add(Icon(Icons.close, color: Colors.red));
      }
      if (quizBrain.isFinished()) {
        var correctCount = scoreKeeper.where((element) => element.color == Colors.green).length;
        Alert(
          context: context,
          type: AlertType.success,
          title: 'Finished!',
          desc: '$correctCount/${scoreKeeper.length}',
          buttons: [
            DialogButton(
              child: Text('Done', style: TextStyle(color: Colors.white, fontSize: 20)),
              onPressed: () {
                Navigator.pop(context);
              },
              width: 120,
            )
          ],
        ).show();
        quizBrain.reset();
        scoreKeeper.clear();
      } else {
        quizBrain.nextQuestion();
      }
    });
  }

Firstly thanks @spaghett1c0de for the logic. Added to that, if you @chintanrparmar are really keen on seeing the 13th icon displayed, here is a slight modification:

  1. Define the following method before the definition of checkAnswer( )

setState(() {
// This has to be done first so it populates the last question before it checks if it finishes
if (userPickedAnswer == correctAnswer) {
scoreKeeper.add(Icon(Icons.check, color: Colors.green));
} else {
scoreKeeper.add(Icon(Icons.close, color: Colors.red));
}
if (quizBrain.isFinished()) {
correctCount = scoreKeeper.where((element) => element.color == Colors.green).length;
reset();
} else {
quizBrain.nextQuestion();
}
});

####################################################################################################

  1. Replace the following part

if (quizBrain.isFinished()) {
var correctCount = scoreKeeper.where((element) => element.color == Colors.green).length;
Alert(
context: context,
type: AlertType.success,
title: 'Finished!',
desc: '$correctCount/${scoreKeeper.length}',
buttons: [
DialogButton(
child: Text('Done', style: TextStyle(color: Colors.white, fontSize: 20)),
onPressed: () {
Navigator.pop(context);
},
width: 120,
)
],
).show();
quizBrain.reset();
scoreKeeper.clear();

with:

reset( );

######################################################################################################

  1. Make sure you make correctCount accessible by declaring it in the beginning just below the scoreKeeper[ ]

@chintanrparmar make sure to close the issue if you are satisfied so that the others could know that we have a solution.