Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,460 --> 00:00:10,170
With the questions now being re-added as maps, in order to pass down our questionText here, it's not
2
00:00:10,170 --> 00:00:13,080
enough to select a question by index, instead
3
00:00:13,080 --> 00:00:18,180
now in there we of course have our questionText key which we're now interested in. So we're interested
4
00:00:18,180 --> 00:00:21,970
in the value of course but we do access this value through its key.
5
00:00:22,020 --> 00:00:27,450
Now how do you tell Dart that you want to get the value for a specific key? Down here,
6
00:00:27,470 --> 00:00:32,120
I'm selecting a specific question in my questions list and that's important,
7
00:00:32,120 --> 00:00:34,940
this list here is still a list.
8
00:00:35,090 --> 00:00:37,730
It's a list which now holds maps
9
00:00:37,730 --> 00:00:46,610
and in this map, we have a combination of string keys and then various objects and Dart is inferring
10
00:00:46,610 --> 00:00:51,210
that we have a map with object values here because we have a mixture of values,
11
00:00:51,230 --> 00:00:52,610
here we have a string value
12
00:00:52,640 --> 00:00:58,460
and here we have a list value and therefore, Dart goes back to the generic parent type which every object
13
00:00:58,460 --> 00:01:02,240
has in Dart and that would be object because everything is an object.
14
00:01:02,840 --> 00:01:08,260
So we have a list of maps where every map has a string key and different kinds of values.
15
00:01:08,300 --> 00:01:10,610
How do we get access to the value now?
16
00:01:10,610 --> 00:01:17,120
Here we're accessing a specific question through its index and therefore now, this here yields us
17
00:01:17,120 --> 00:01:19,620
a map, for index 0,
18
00:01:19,620 --> 00:01:23,530
it yields us the first map, for index one, it yields us the second map.
19
00:01:23,580 --> 00:01:28,270
If we now want to get access to a specific key, we again use square brackets here,
20
00:01:28,290 --> 00:01:30,990
so not curly braces but square brackets,
21
00:01:31,080 --> 00:01:37,380
we use square brackets to access a key in a map and there we simply use the key name in here, inside of the square
22
00:01:37,380 --> 00:01:38,500
brackets.
23
00:01:38,520 --> 00:01:44,760
So on all the questions, I'm accessing the question with index 0, 1, 2 and so on
24
00:01:44,970 --> 00:01:51,450
and then here for the chosen question map, I access the value with the key, questionText.
25
00:01:51,450 --> 00:01:54,450
So that means I access this value,
26
00:01:54,450 --> 00:02:03,790
this value or this value and it's this string which then gets forwarded to the question, awesome. We can
27
00:02:03,790 --> 00:02:07,960
forward our value to the question and
28
00:02:08,050 --> 00:02:10,600
that's of course pretty neat.
29
00:02:10,630 --> 00:02:17,530
Now how can we create our answers dynamically based on the selected question?
30
00:02:17,860 --> 00:02:21,890
We always have four answers, so we could hardcode our answers here
31
00:02:22,030 --> 00:02:27,310
but of course, we could also have one question with four answers and another question with only three
32
00:02:27,310 --> 00:02:28,110
answers.
33
00:02:28,360 --> 00:02:34,090
So it would be nice if we could create these answer widgets dynamically based on the current question
34
00:02:34,930 --> 00:02:40,720
and that of course can be done with Dart. Instead of hardcoding the answer widgets here, what you can
35
00:02:40,720 --> 00:02:51,460
do is you can access your questions here, so access questions which is a list
36
00:02:51,520 --> 00:02:52,510
and now
37
00:02:52,570 --> 00:02:54,940
and that's really nice, map
38
00:02:54,940 --> 00:02:58,180
that list into something else.
39
00:02:58,180 --> 00:03:04,510
That means transform that list into something else so that instead of a list of maps, we have a list
40
00:03:04,510 --> 00:03:11,080
of widgets and that list of widgets could then be added here and merged into our list of widgets in
41
00:03:11,080 --> 00:03:12,250
that column.
42
00:03:12,250 --> 00:03:14,630
For that, you have the map method.
43
00:03:14,770 --> 00:03:21,910
Again, questions is a list and a list is a class, an object in Dart and that class defines a map method.
44
00:03:22,300 --> 00:03:30,430
The map method executes a function which you have to pass as an argument to map on every element in
45
00:03:30,430 --> 00:03:32,170
the list on which you're calling map,
46
00:03:32,170 --> 00:03:37,960
so in this case on every element in the questions list. So we have to pass a function to map and you
47
00:03:37,960 --> 00:03:41,440
could use a named function which you could create up here
48
00:03:41,440 --> 00:03:47,350
or you use an anonymous function since we really only need that function here and in no other place
49
00:03:47,350 --> 00:03:48,810
of our file.
50
00:03:48,880 --> 00:03:54,730
So here, I'll define a function and that function automatically receives an argument itself which will be
51
00:03:54,730 --> 00:03:59,620
passed in by Dart and that's the current element we're looking at because this function will be executed
52
00:03:59,620 --> 00:04:05,650
for every element in the questions list and the current element for the current function execution, that
53
00:04:05,650 --> 00:04:10,450
is received as an argument inside of this function which is applied to every element.
54
00:04:10,450 --> 00:04:17,440
So here, we get the question, single question we're looking at and now in here, we have to return a new
55
00:04:17,440 --> 00:04:24,280
value because map returns us basically a new list where we don't have to keep the old structure,
56
00:04:24,430 --> 00:04:29,620
so it doesn't have to be a list of maps, it can be a list of widgets and that could be something we merge
57
00:04:29,620 --> 00:04:31,480
here into the column.
58
00:04:31,480 --> 00:04:36,700
Now for that, we want to return a widget here because we want to, for every element in questions, we want
59
00:04:36,700 --> 00:04:39,400
to return a new element which basically replaces it
60
00:04:39,490 --> 00:04:40,530
and that should be a widget
61
00:04:40,530 --> 00:04:45,710
now. So here, I want to create a new answer widget
62
00:04:46,110 --> 00:04:54,810
and to that answer widget, I want to forward question, however we're actually calling map on the wrong
63
00:04:54,810 --> 00:04:58,890
thing now because questions is a list of maps, right?
64
00:04:58,980 --> 00:05:03,780
Actually I want to go through all my answers for the question we're currently looking at.
65
00:05:04,380 --> 00:05:06,270
So we should fine tune this a little bit
66
00:05:06,330 --> 00:05:13,560
and here we access questions for the currently selected question index and there, we now select a key
67
00:05:14,040 --> 00:05:16,820
and that key now should be answers
68
00:05:16,860 --> 00:05:21,390
and that will then give us a list of answers and that's actually what I'm interested in here.
69
00:05:21,420 --> 00:05:28,760
So map now takes a function which runs on all our answers and hence in here, I get answer and I forward
70
00:05:28,800 --> 00:05:33,180
that answer to my answer widget.
71
00:05:33,180 --> 00:05:39,240
Now here I'm getting an error that map isn't defined on the class object and the problem we're having
72
00:05:39,240 --> 00:05:46,690
here is that Dart fails to infer that the answers key always holds a list.
73
00:05:46,860 --> 00:05:54,420
It does here but Dart simply doesn't see that, it simply is something Dart can't detect.
74
00:05:54,780 --> 00:06:03,990
Now to fix that, we can tell Dart that answers is a list by wrapping this part all the way up to after
75
00:06:04,200 --> 00:06:05,630
this answers
76
00:06:05,860 --> 00:06:12,120
key we're accessing here into parentheses and adding the as keyword which allows us to tell Dart hey,
77
00:06:12,300 --> 00:06:14,340
this will have a specific type,
78
00:06:14,370 --> 00:06:21,180
I know it and we know that this will be of type list full of strings, string should have a capital
79
00:06:21,180 --> 00:06:23,680
S.
80
00:06:23,720 --> 00:06:31,950
So now we tell Dart that answer here, answers which we access, will actually be a list of strings and the
81
00:06:31,960 --> 00:06:34,150
error we're now getting is a different error which we'll fix later,
82
00:06:34,150 --> 00:06:34,870
no worries.
83
00:06:36,210 --> 00:06:44,790
This however will work now, we can map a list of strings. Answer here therefore is a string and to answer,
84
00:06:45,230 --> 00:06:47,250
we forward that string.
85
00:06:47,250 --> 00:06:51,900
Now of course at the moment, answer doesn't take a string, it only takes this function handler,
86
00:06:51,900 --> 00:06:58,320
so we should accept a second argument here in the answer widget which is the answer text one output
87
00:06:58,320 --> 00:07:02,970
and I also want to store this in a property here,
88
00:07:03,000 --> 00:07:08,910
answer text and therefore actually here in the constructor list, let's use this answer text which then
89
00:07:08,940 --> 00:07:12,750
automatically assigns the incoming value to this property
90
00:07:12,870 --> 00:07:20,350
and the answer text is then what we can use down there in our answer widget. So now, we're accepting
91
00:07:20,350 --> 00:07:26,110
answer text but still here when we create the widget, the answer text which is inside of an answer is
92
00:07:26,110 --> 00:07:28,080
not the first value we should pass to
93
00:07:28,120 --> 00:07:34,930
the answer widget but the second one, the first value should be our handler for the chosen answer and
94
00:07:34,930 --> 00:07:41,390
for that, we of course still want to pass a pointer to our _answerQuestion function here.
95
00:07:41,470 --> 00:07:46,220
So let's forward this for every answer which we're creating.
96
00:07:46,370 --> 00:07:49,970
That was a lot of work and I know that this can be a confusing setup here,
97
00:07:49,970 --> 00:07:52,720
mapping a list into a list of widgets,
98
00:07:52,730 --> 00:07:54,170
what's the idea here?
99
00:07:54,170 --> 00:07:59,360
Well the idea really is that now we have a list of widgets here in the end, at least almost because
100
00:07:59,360 --> 00:08:05,090
map doesn't give us a list but a so-called iterable which is kind of a parent class for all the different
101
00:08:05,110 --> 00:08:05,940
iterables,
102
00:08:06,020 --> 00:08:11,510
a map would also be an iterable and to really tell Dart that we want a list here and not a map or anything
103
00:08:11,510 --> 00:08:16,700
else, we can add a dot after this whole operation and call to list
104
00:08:16,700 --> 00:08:20,310
and now this converts the value map gives us to a list.
105
00:08:20,310 --> 00:08:25,390
Also please note that map does not change the original questions list,
106
00:08:25,490 --> 00:08:28,520
it does not change this variable or the value in there,
107
00:08:28,610 --> 00:08:35,000
it generates a new list instead based on the old list and then the transformation we do in the body
108
00:08:35,000 --> 00:08:38,000
of that function will be passed to map.
109
00:08:38,000 --> 00:08:44,270
The remaining problem now is that we have a list here and here, column also takes a list of widgets but
110
00:08:44,270 --> 00:08:46,120
with that, we would have a nested list,
111
00:08:46,160 --> 00:08:53,650
we would add a list in a list. Actually, we want to have a couple of individual items in that list of
112
00:08:53,650 --> 00:09:01,110
children and to get that, we can use another operator that's available in Dart, it's a so-called spread operator.
113
00:09:01,300 --> 00:09:07,240
You can add three dots in front of this and now I get a warning because it's a relatively new operator
114
00:09:07,300 --> 00:09:13,070
but it is supported in the recent versions of Dart and what this spread operator does,
115
00:09:13,150 --> 00:09:20,470
what these three dots do here is they take a list which is exactly what we have here and they pull all
116
00:09:20,470 --> 00:09:28,840
the values in the list out of it and add them to the surrounding list as individual values, so that we
117
00:09:28,840 --> 00:09:30,710
don't add a list to a list
118
00:09:30,790 --> 00:09:37,540
but the values of a list to a list, having only one list without a nested list.
119
00:09:37,570 --> 00:09:38,970
So this is what we're doing here
120
00:09:39,100 --> 00:09:45,580
and with that, we're having a transformation where we transform every answer into an answer widget.
121
00:09:45,580 --> 00:09:50,770
We then make sure that this is a list and then we take these generated answer widgets and we add it to
122
00:09:50,770 --> 00:09:53,880
this list here.
123
00:09:53,920 --> 00:10:01,450
That was a lot of work of course but with that if we save this, we indeed see four values here for our
124
00:10:01,450 --> 00:10:02,250
four answers
125
00:10:02,260 --> 00:10:07,930
and if I choose one and we go to the next question, you'll see that these answers change and if I click again
126
00:10:07,930 --> 00:10:09,870
since I already was on the second question,
127
00:10:09,870 --> 00:10:14,580
now I get an error because now I try to advance to a question which we don't have.
128
00:10:14,680 --> 00:10:19,190
Of course, we could implement checks that avoids that we can go to a question which we don't have
129
00:10:19,330 --> 00:10:21,700
but for the moment, I'm happy with the current state.
14099
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.