Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,570 --> 00:00:07,680
So notice that all that we did, is we created a list of questions where we copied over the questions
1
2
00:00:07,740 --> 00:00:10,760
from down here and we've only got three of them.
2
3
00:00:10,950 --> 00:00:16,260
And then we created a variable to track where we are in the list of questions,
3
4
00:00:16,500 --> 00:00:23,610
and it starts from zero. And then down here where we have our text widget, the text that we chose to display
4
5
00:00:23,610 --> 00:00:31,080
in it comes from our list of questions pulling in the item at this particular index. And then down
5
6
00:00:31,080 --> 00:00:37,980
here, in our onPressed for our true and false buttons, we use set state to update our question number
6
7
00:00:38,100 --> 00:00:43,750
and update the screen when the user presses the true or the false buttons.
7
8
00:00:43,770 --> 00:00:46,440
Now we can use the hot restart,
8
9
00:00:46,470 --> 00:00:53,040
then we reset our question number back down to zero. And we can cycle through our questions until we
9
10
00:00:53,040 --> 00:00:59,250
run out of questions at which point we get a range error because we don't have any more questions to
10
11
00:00:59,250 --> 00:01:00,540
display.
11
12
00:01:00,540 --> 00:01:06,730
So now that we've done that, the next part is to create a list of matching answers.
12
13
00:01:06,930 --> 00:01:13,470
So right below our questions here, we're going to create a list of booleans, because these are going to
13
14
00:01:13,470 --> 00:01:14,850
be our answers.
14
15
00:01:15,060 --> 00:01:22,830
And this is also going to be a list, and it's going to correspond to the questions. So we can see that
15
16
00:01:22,830 --> 00:01:26,340
the first question's correct answer is false,
16
17
00:01:26,340 --> 00:01:29,290
and the second question is true and then the third is true.
17
18
00:01:29,700 --> 00:01:32,990
So in our list of answers, we're going to listed in that order.
18
19
00:01:33,000 --> 00:01:36,930
So again we say false, true, true.
19
20
00:01:36,990 --> 00:01:44,470
So now, at the same index, our answers match our questions. At index 0
20
21
00:01:44,520 --> 00:01:48,770
is this question and it's correct answer is false.
21
22
00:01:48,790 --> 00:01:55,600
So now we can check to see if the user got the right answer. Inside our onPressed,
22
23
00:01:55,630 --> 00:02:02,050
we're going to figure out if the user got the answer right or if they got it wrong. In order to do this,
23
24
00:02:02,080 --> 00:02:04,680
we're first going to figure out what the correct answer was.
24
25
00:02:05,140 --> 00:02:10,550
So let's create a new variable and it's going to hold data type of boolean.
25
26
00:02:10,990 --> 00:02:14,050
And this is going to be called correct answer.
26
27
00:02:14,380 --> 00:02:22,270
Now the correct answer of course comes from our list of answers, and we're going to pull out the answer
27
28
00:02:22,480 --> 00:02:24,250
at the current question number.
28
29
00:02:24,340 --> 00:02:32,360
So we're gonna say answer, and inside a set of square brackets, we're going to provide the question number.
29
30
00:02:32,380 --> 00:02:38,290
So that means that when the user is seeing 'You can lead a cow downstairs but not upstairs', then
30
31
00:02:38,290 --> 00:02:43,640
question number has to be equal to 0, because it starts off being 0.
31
32
00:02:43,990 --> 00:02:51,880
And so when we look inside the answers list and we try to get the one at index 0, then that's the correct
32
33
00:02:51,940 --> 00:02:55,300
answer for that question.
33
34
00:02:55,330 --> 00:03:02,380
So now that we've got the correct answer, all we have to do is to check to see if what the user picked,
34
35
00:03:02,440 --> 00:03:06,940
which in this case is true, is equal to the correct answer.
35
36
00:03:07,870 --> 00:03:13,840
So we can use something called an if-statement, and inside these parentheses, we're going to check to
36
37
00:03:13,840 --> 00:03:21,670
see if the user-picked answer, which in this case is true, is equal to the correct answer.
37
38
00:03:21,880 --> 00:03:29,510
So we can say correctAnswer == true. Then that means the user got it right.
38
39
00:03:29,530 --> 00:03:35,500
So we can print into the console, we can write user got it right.
39
40
00:03:38,140 --> 00:03:45,940
But if they didn't get it right, then we're going to print user got it wrong.
40
41
00:03:45,940 --> 00:03:53,320
If the correct answer is equal to the button that they picked, which is true, then we're going to print
41
42
00:03:53,320 --> 00:03:54,280
they got it right,
42
43
00:03:54,280 --> 00:03:55,440
otherwise we gonna print
43
44
00:03:55,450 --> 00:03:57,040
they got it wrong.
44
45
00:03:57,040 --> 00:04:02,260
And then down here at the other button, and this is the false button,
45
46
00:04:02,260 --> 00:04:10,150
if they clicked on this button then we want to check if the correct answer is equal to false. Then that
46
47
00:04:10,150 --> 00:04:11,320
means they got it right,
47
48
00:04:11,380 --> 00:04:14,470
otherwise it means they got it wrong.
48
49
00:04:14,490 --> 00:04:21,690
Now you have to make sure that this set of code goes before we increase the question number and we go
49
50
00:04:21,690 --> 00:04:28,380
to the next question. Because we want to check the correct answer for the current question and make sure
50
51
00:04:28,380 --> 00:04:30,050
that those two match.
51
52
00:04:30,090 --> 00:04:38,670
Now if we go ahead and click hot restart to take our question number back down to zero and start on question
52
53
00:04:38,670 --> 00:04:39,220
1,
53
54
00:04:39,420 --> 00:04:46,830
then you can see that this is the question. And if I click on true, that's my selection, then it tells
54
55
00:04:46,830 --> 00:04:52,860
me that I got it wrong because the actual correct answer for the first question is false.
55
56
00:04:52,860 --> 00:04:54,300
Now let's go onto the next one.
56
57
00:04:54,300 --> 00:04:55,510
Let's try and get this one right.
57
58
00:04:55,540 --> 00:04:57,210
Let's check out the answer.
58
59
00:04:57,210 --> 00:04:59,490
So this one, the correct answer is true.
59
60
00:04:59,490 --> 00:05:02,800
So if I click on the true button, it tells me that I got it right.
60
61
00:05:03,060 --> 00:05:04,830
And the final one is also a true.
61
62
00:05:04,830 --> 00:05:11,160
So if I click on the false button, it tells me that I got it wrong, but then it crashes because it tries
62
63
00:05:11,160 --> 00:05:15,210
to go to the next question, and we don't have another question.
63
64
00:05:15,210 --> 00:05:24,390
So although this works, if we had more data for each question, say if we had instead of just true and
64
65
00:05:24,390 --> 00:05:30,870
false, we had you know a multiple choice question or for each question we even had a picture that's associated
65
66
00:05:30,870 --> 00:05:37,800
with it, then we would end up creating many many lists and try to make sure that we're always matching
66
67
00:05:37,860 --> 00:05:42,180
the right item in these different lists.
67
68
00:05:42,240 --> 00:05:44,120
And that's quite error prone.
68
69
00:05:44,190 --> 00:05:49,270
So we're going to upgrade our questions and answers a little bit later on.
69
70
00:05:49,380 --> 00:05:57,390
But first for those you guys who have never seen this syntax these if and else, or what we call conditionals
70
71
00:05:57,420 --> 00:06:04,620
in programming, then the next lesson is all about that and we discuss how to use if and else statements in
71
72
00:06:04,620 --> 00:06:09,400
Dart, and how to build up an elseif leader using Dart as well.
72
73
00:06:09,420 --> 00:06:16,080
So if this is unfamiliar to you, then you might find the next lesson really helpful to get a better understanding
73
74
00:06:16,140 --> 00:06:16,850
of this.
74
75
00:06:16,950 --> 00:06:22,100
But if you're already familiar with how if and else works, then feel free to skip to the next lesson where
75
76
00:06:22,110 --> 00:06:25,920
we're going to build a class for our questions.
8516
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.