So notice that all that we did, is we created a list of questions where we copied over the questions
from down here and we've only got three of them.
And then we created a variable to track where we are in the list of questions,
and it starts from zero. And then down here where we have our text widget, the text that we chose to display
in it comes from our list of questions pulling in the item at this particular index. And then down
here, in our onPressed for our true and false buttons, we use set state to update our question number
and update the screen when the user presses the true or the false buttons.
Now we can use the hot restart,
then we reset our question number back down to zero. And we can cycle through our questions until we
run out of questions at which point we get a range error because we don't have any more questions to
display.
So now that we've done that, the next part is to create a list of matching answers.
So right below our questions here, we're going to create a list of booleans, because these are going to
be our answers.
And this is also going to be a list, and it's going to correspond to the questions. So we can see that
the first question's correct answer is false,
and the second question is true and then the third is true.
So in our list of answers, we're going to listed in that order.
So again we say false, true, true.
So now, at the same index, our answers match our questions. At index 0
is this question and it's correct answer is false.
So now we can check to see if the user got the right answer. Inside our onPressed,
we're going to figure out if the user got the answer right or if they got it wrong. In order to do this,
we're first going to figure out what the correct answer was.
So let's create a new variable and it's going to hold data type of boolean.
And this is going to be called correct answer.
Now the correct answer of course comes from our list of answers, and we're going to pull out the answer
at the current question number.
So we're gonna say answer, and inside a set of square brackets, we're going to provide the question number.
So that means that when the user is seeing 'You can lead a cow downstairs but not upstairs', then
question number has to be equal to 0, because it starts off being 0.
And so when we look inside the answers list and we try to get the one at index 0, then that's the correct
answer for that question.
So now that we've got the correct answer, all we have to do is to check to see if what the user picked,
which in this case is true, is equal to the correct answer.
So we can use something called an if-statement, and inside these parentheses, we're going to check to
see if the user-picked answer, which in this case is true, is equal to the correct answer.
So we can say correctAnswer == true. Then that means the user got it right.
So we can print into the console, we can write user got it right.
But if they didn't get it right, then we're going to print user got it wrong.
If the correct answer is equal to the button that they picked, which is true, then we're going to print
they got it right,
otherwise we gonna print
they got it wrong.
And then down here at the other button, and this is the false button,
if they clicked on this button then we want to check if the correct answer is equal to false. Then that
means they got it right,
otherwise it means they got it wrong.
Now you have to make sure that this set of code goes before we increase the question number and we go
to the next question. Because we want to check the correct answer for the current question and make sure
that those two match.
Now if we go ahead and click hot restart to take our question number back down to zero and start on question
1,
then you can see that this is the question. And if I click on true, that's my selection, then it tells
me that I got it wrong because the actual correct answer for the first question is false.
Now let's go onto the next one.
Let's try and get this one right.
Let's check out the answer.
So this one, the correct answer is true.
So if I click on the true button, it tells me that I got it right.
And the final one is also a true.
So if I click on the false button, it tells me that I got it wrong, but then it crashes because it tries
to go to the next question, and we don't have another question.
So although this works, if we had more data for each question, say if we had instead of just true and
false, we had you know a multiple choice question or for each question we even had a picture that's associated
with it, then we would end up creating many many lists and try to make sure that we're always matching
the right item in these different lists.
And that's quite error prone.
So we're going to upgrade our questions and answers a little bit later on.
But first for those you guys who have never seen this syntax these if and else, or what we call conditionals
in programming, then the next lesson is all about that and we discuss how to use if and else statements in
Dart, and how to build up an elseif leader using Dart as well.
So if this is unfamiliar to you, then you might find the next lesson really helpful to get a better understanding
of this.
But if you're already familiar with how if and else works, then feel free to skip to the next lesson where
we're going to build a class for our questions.