Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,440 --> 00:00:12,150
Now for a start, to change something on screen, let's make sure that we output a real question here.
2
00:00:12,160 --> 00:00:14,750
We got two questions here in this list of questions
3
00:00:14,750 --> 00:00:17,850
and I want to start with my first question here
4
00:00:17,980 --> 00:00:23,080
at the beginning of the app but once we pressed a button and for the moment, it doesn't matter which
5
00:00:23,080 --> 00:00:24,280
button we pressed,
6
00:00:24,280 --> 00:00:27,910
I want to switch to that second element here. Now for that,
7
00:00:27,910 --> 00:00:31,450
let's first of all start about putting the first element. So I don't want to hardcode
8
00:00:31,450 --> 00:00:36,900
my question down there into this text widget but instead, I want to use the first element of my questions
9
00:00:36,910 --> 00:00:40,020
list. To do that,
10
00:00:40,240 --> 00:00:46,270
we first of all have to refer to questions, questions is the name of the variable by adding it here
11
00:00:46,300 --> 00:00:52,480
but now questions is a list of strings and I only want to take the first string and output it. To do
12
00:00:52,480 --> 00:01:00,450
that, Dart gives us a syntax for accessing the different items in a list with their different indexes.
13
00:01:01,030 --> 00:01:05,590
Each item here has an index, the index starts at zero for the first item,
14
00:01:05,590 --> 00:01:08,260
that's important, not at one but at zero
15
00:01:08,380 --> 00:01:11,190
and we can access the different elements by their index.
16
00:01:11,230 --> 00:01:12,460
There are two ways of doing that,
17
00:01:12,490 --> 00:01:17,060
you can add a dot here after your variable names and now since questions
18
00:01:17,060 --> 00:01:19,570
in the end holds a list which is an object,
19
00:01:19,570 --> 00:01:25,690
this has quite a lot of methods which Dart gives us automatically because list is a default object built
20
00:01:25,690 --> 00:01:31,390
into Dart and it's a more complex object as you can clearly tell with a lot of built-in methods.
21
00:01:31,390 --> 00:01:37,330
Now we'll use some of these methods throughout the course, here one method we could use is elementAt
22
00:01:37,980 --> 00:01:44,530
and now here we could pass in an index, which if you hover over that, needs to be an integer
23
00:01:44,530 --> 00:01:52,100
and if that is zero for example, you get access to the element at the index 0 which is the first element.
24
00:01:52,180 --> 00:01:57,610
So if I do that and I save this, you see what's your favorite color here
25
00:01:57,730 --> 00:02:02,250
because what's your favorite color is the first element here and therefore, it has the index 0.
26
00:02:02,320 --> 00:02:03,310
If I pass one in here,
27
00:02:03,310 --> 00:02:04,660
we would print what's your favorite
28
00:02:04,660 --> 00:02:08,290
animal, animal should be the text here of course.
29
00:02:08,410 --> 00:02:13,820
If I pass two in here, which is index two, then we actually get an error here right.
30
00:02:13,840 --> 00:02:22,890
I get an error that we scroll up, that we have a range error here, invalid value not in range 0 to
31
00:02:22,890 --> 00:02:28,770
1, so that's the range we have and we try to use two and that's in the end the error I get here because I try
32
00:02:28,770 --> 00:02:34,710
to access the element at an index which we don't have in here because the index at the element 2 would
33
00:02:34,710 --> 00:02:39,160
be the third element in this array and we only have two.
34
00:02:39,630 --> 00:02:43,380
So that's one way of doing this but there is a shorter way, instead of using elementAt,
35
00:02:43,380 --> 00:02:48,580
you can add square brackets after the variable name and then enter your index number there.
36
00:02:48,630 --> 00:02:52,920
That's doing the exact same thing but it's a bit shorter and therefore, it's the syntax you typically
37
00:02:52,920 --> 00:02:57,740
use. This now accesses the first element in the questions list and therefore this
38
00:02:57,750 --> 00:02:59,430
what's your favorite color question
39
00:02:59,440 --> 00:03:01,020
and hence this, what we now see here.
40
00:03:02,760 --> 00:03:03,960
So that's nice,
41
00:03:03,990 --> 00:03:09,570
however it would be nice if we could change that index which we're accessing dynamically when we press
42
00:03:09,570 --> 00:03:17,250
a button. To make that happen, we can add a new variable, now however not inside of the build method here
43
00:03:17,700 --> 00:03:23,880
because then it would essentially be reset and change every time build runs and actually Flutter executes
44
00:03:23,880 --> 00:03:30,570
build a couple of times, it always executes build when it needs to rebuild the interface on the screen,
45
00:03:31,380 --> 00:03:31,820
instead
46
00:03:31,860 --> 00:03:37,770
I want to add it as a class wide variable. So here inside my class, I can add a new variable at the top
47
00:03:38,580 --> 00:03:41,260
and that could be question
48
00:03:41,280 --> 00:03:42,030
index
49
00:03:44,550 --> 00:03:46,110
which is 0 initially.
50
00:03:46,140 --> 00:03:52,230
Now we could also replace var with int but since I initialize it with 0, Dart is able to infer this and
51
00:03:52,230 --> 00:03:55,080
hence using var is a better practice.
52
00:03:55,080 --> 00:03:57,510
Now by adding this property here,
53
00:03:57,540 --> 00:04:04,710
so a class wide variable is called a property as you learned, we can use it here in answerQuestion and
54
00:04:05,250 --> 00:04:15,350
I could take my question index and set it equal to one or simply to question index plus one to use
55
00:04:15,350 --> 00:04:20,390
the old value that's stored in question index and then add one to it and then after this calculation
56
00:04:20,390 --> 00:04:23,110
is done, that's stored back into question index.
57
00:04:23,120 --> 00:04:27,490
So the old value of question index will be overriden with the old value plus one
58
00:04:29,020 --> 00:04:31,150
and I can remove answer chosen now.
59
00:04:31,700 --> 00:04:39,170
So we could update question index here and now here when we output a question, instead of using 0, we
60
00:04:39,170 --> 00:04:41,710
could refer to question index here.
61
00:04:41,750 --> 00:04:50,180
So the index which we use to access an element in our questions list is set dynamically, it is 0 initially
62
00:04:50,540 --> 00:04:58,070
and we increment it to 1 after a question was answered and right now answerQuestion is only connected
63
00:04:58,070 --> 00:04:59,240
to the first button.
64
00:04:59,300 --> 00:05:04,250
So when we press the first button, we should change question index and therefore,
65
00:05:04,250 --> 00:05:10,850
we should also see our question change here, to the second question which is one for our favorite
66
00:05:10,880 --> 00:05:17,870
animal. Let's also print question index here to see whether that works. If we save this,
67
00:05:21,300 --> 00:05:22,370
it should rebuild,
68
00:05:22,380 --> 00:05:28,950
you can always manually press that flash button here to do a hot reload or if somehow your app got stuck,
69
00:05:28,950 --> 00:05:34,890
you do a hot restart to fully restart your app which takes a tiny bit longer but make sure that you
70
00:05:34,890 --> 00:05:43,900
really rebuild your app with the latest piece of code and now if press answer 1, seems to work.
71
00:05:43,910 --> 00:05:51,140
If I go back, I see one and two because I pressed it twice but if I only press it once, let me rebuild
72
00:05:51,170 --> 00:05:55,720
this again to really prove that this only executes once,
73
00:05:55,750 --> 00:06:01,660
so if I now press answer 1, I see one here.
74
00:06:01,930 --> 00:06:02,580
So this works
75
00:06:02,590 --> 00:06:05,950
but what you also see is that the text didn't change,
76
00:06:05,950 --> 00:06:07,990
it's still what's your favorite color,
77
00:06:07,990 --> 00:06:11,590
it did not change to what's your favorite animal.
78
00:06:11,590 --> 00:06:12,780
So what's wrong here?
79
00:06:12,790 --> 00:06:16,260
Do we have an error in our code?
80
00:06:16,270 --> 00:06:16,720
No,
81
00:06:16,720 --> 00:06:21,230
the problem is and that's also kind of signaled here by the green squiggly lines
82
00:06:21,400 --> 00:06:23,200
but that's a bit too cryptic
83
00:06:23,200 --> 00:06:30,310
but the problem is in the end that we're trying to change some internal state of this widget and for that
84
00:06:30,310 --> 00:06:34,690
of course, we need to understand what state is and then we'll understand what the remedy is.
9005
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.