All language subtitles for 8. Creating a Question Class

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French Download
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,460 --> 00:00:00,900 All right. 1 2 00:00:00,960 --> 00:00:09,090 So now that our app is able to show the questions in our list of questions and also match up the questions 2 3 00:00:09,180 --> 00:00:13,890 with the answers to check to see if the user got the right answer. 3 4 00:00:14,580 --> 00:00:16,910 But at the moment it's a bit clunky. 4 5 00:00:16,920 --> 00:00:23,790 We've got two lists that are tracking things that should be associated with each other because this 5 6 00:00:23,790 --> 00:00:31,620 is the answer to this question and ideally it would be great if they were stored together somehow. 6 7 00:00:31,620 --> 00:00:33,410 So how can we do this? 7 8 00:00:33,450 --> 00:00:41,460 Well we can create a new class. Just as we have our widgets which have different properties such as a 8 9 00:00:41,460 --> 00:00:46,590 child property or a padding property or a flex property, 9 10 00:00:46,590 --> 00:00:54,120 we can also create a class that have a question property and a answer property, so that we can associate 10 11 00:00:54,510 --> 00:01:00,080 the questions and answers together in a single object. In order to do this, 11 12 00:01:00,090 --> 00:01:05,880 we're going to go into our lib folder where all of the action happens, and we're going to right click 12 13 00:01:06,090 --> 00:01:07,570 and go to new. 13 14 00:01:07,600 --> 00:01:14,790 I'm going to select to create a new Dart file, and you're going to call this Dart file question, because 14 15 00:01:14,970 --> 00:01:17,670 that's exactly what it's going to represent. 15 16 00:01:17,670 --> 00:01:22,590 And then it says, 'do you want to add this to git?' You can either click yes or no. 16 17 00:01:22,600 --> 00:01:28,160 It doesn't really matter in this case because we're not really using git for this project. 17 18 00:01:28,200 --> 00:01:34,600 So in order to create a new class, we have to use the keyword class. 18 19 00:01:34,740 --> 00:01:38,760 And afterwards we give our class a name. 19 20 00:01:38,760 --> 00:01:45,710 So in this case, we're going to call our class question. And it's by convention that when you create a 20 21 00:01:45,710 --> 00:01:54,500 new class, it starts with a capital letter just as all our classes down here, say our Quiz page or our 21 22 00:01:54,500 --> 00:02:00,850 widgets all start with a capital letter, because they each belong to a different class. 22 23 00:02:00,890 --> 00:02:07,160 So over here, we've created our question class and we're going to open a set of parentheses to define 23 24 00:02:07,250 --> 00:02:14,240 what is so special about this question class. The question class is only going to have two properties, 24 25 00:02:14,870 --> 00:02:18,560 and they're going to be properties that every question should have. 25 26 00:02:18,560 --> 00:02:26,390 So one is going to be a string and it's going to be called the questionText, and the other is going to 26 27 00:02:26,390 --> 00:02:31,640 be a boolean and it's going to be questionAnswer. 27 28 00:02:31,640 --> 00:02:39,320 Now inside this question, we also have to give these variables a value when we create a new question. 28 29 00:02:39,920 --> 00:02:41,030 In order to do that, 29 30 00:02:41,090 --> 00:02:47,450 we have to create what's called a constructor. So that when we construct this question object, we give 30 31 00:02:47,450 --> 00:02:55,010 it some values for the question text and question answer. And we do this by writing, the same as our class 31 32 00:02:55,010 --> 00:03:04,040 name, question. And then we open a set of parentheses and then I'm going to add some curly braces and 32 33 00:03:04,040 --> 00:03:06,800 this is beginning to look a bit like our functions. 33 34 00:03:06,800 --> 00:03:12,770 This is where our inputs would normally go. Well in this case these are the values that we're going to 34 35 00:03:12,770 --> 00:03:15,470 provide when we create a new question. 35 36 00:03:15,500 --> 00:03:22,670 So we're going to have a string that's going to be called q, and we're going to have a bool that's going 36 37 00:03:22,670 --> 00:03:26,940 to be called a. And you can see where I'm going here. 37 38 00:03:27,180 --> 00:03:35,010 Inside these parentheses that I've just opened up, we're going to set this question's questionText to 38 39 00:03:35,010 --> 00:03:42,810 equal the value of q, and then we're going to set the question answer to equal the value of a. 39 40 00:03:42,930 --> 00:03:45,320 And this part of the code has a special name. 40 41 00:03:45,360 --> 00:03:48,060 It's called a constructor. 41 42 00:03:48,060 --> 00:03:57,990 So now that we have this structure for a question object, we can create a new question object in our 42 43 00:03:57,990 --> 00:03:59,480 main.dart 43 44 00:03:59,520 --> 00:04:06,810 So inside here, let's create a new question object and we do that by specifying its type. 44 45 00:04:06,810 --> 00:04:15,360 So this is a new question type. And at the moment, it doesn't know this class question. This is undefined 45 46 00:04:15,360 --> 00:04:18,270 class question, but it's right here 46 47 00:04:18,270 --> 00:04:19,090 right? 47 48 00:04:19,110 --> 00:04:24,060 So how do we tell this file about this file? 48 49 00:04:24,060 --> 00:04:25,820 Well we have to import it. 49 50 00:04:25,950 --> 00:04:27,530 So let's write our inputs. 50 51 00:04:27,570 --> 00:04:31,250 And here we write our question.dart file. 51 52 00:04:31,380 --> 00:04:37,020 So now that we've imported that file, then the error goes away and it now knows about the question in 52 53 00:04:37,020 --> 00:04:37,530 class. 53 54 00:04:38,130 --> 00:04:44,170 So we're creating a new variable which has a data type of question. 54 55 00:04:44,450 --> 00:04:52,260 And this is gonna be called, let's call it q1. And we're going to set it to equal a new question and 55 56 00:04:52,260 --> 00:04:56,860 we're going to use that constructor that we built over here. 56 57 00:04:56,970 --> 00:05:02,360 We're going to provide a q and we're going to provide an a. 57 58 00:05:02,370 --> 00:05:10,560 So when I hit enter, you can see I can tap into the q property where I'm going to put in the first question 58 59 00:05:10,560 --> 00:05:17,880 right here. And this is expecting a string so that satisfies the criteria, and then I'm going to put in 59 60 00:05:18,120 --> 00:05:25,410 a a which is going to be the answer for the first question, which is just a false which is a boolean. 60 61 00:05:26,070 --> 00:05:38,340 So now I've created a new question object from my question constructor which I built over here. And my 61 62 00:05:38,340 --> 00:05:48,330 new question q1, now has some question text which is set to whatever I put in here and it has a question 62 63 00:05:48,360 --> 00:05:52,930 answer which is set to equal what I put in here for the a. 63 64 00:05:52,950 --> 00:06:01,700 So now what I can do is, I can say print q1. question Text or print 64 65 00:06:01,740 --> 00:06:12,990 q1. questionAnswer. And notice how these two pieces of data are all associated with that object 65 66 00:06:12,990 --> 00:06:21,000 that I created up here, and then no longer separate like how we had it up here. And we don't have to make 66 67 00:06:21,000 --> 00:06:28,230 sure that they each match in that order in the list. The data for the text and the answer are now grouped 67 68 00:06:28,290 --> 00:06:31,640 together in this object called 68 69 00:06:31,640 --> 00:06:41,250 q1. Now instead of having our list of questions and answers, here's a challenge for you. 69 70 00:06:41,380 --> 00:06:52,780 Can you figure out how to create a list of questions instead, using the questions and answers that we 70 71 00:06:52,780 --> 00:06:54,190 have down here? 71 72 00:06:55,970 --> 00:06:59,940 So pause the video and see if you can complete this challenge. 72 73 00:07:02,500 --> 00:07:02,930 All right. 73 74 00:07:02,940 --> 00:07:08,350 So let's go ahead and comment out all these lines of code. And right below, 74 75 00:07:08,370 --> 00:07:14,940 we're going to create a new list and the data type of the list are question objects. 75 76 00:07:15,150 --> 00:07:22,410 So it's gonna be a list of questions. And you can call it anything you want but I'm gonna call it a question 76 77 00:07:22,590 --> 00:07:26,920 bank, and I'm gonna set it to equal a new list 77 78 00:07:26,970 --> 00:07:34,380 using that square bracket notation. Now inside the list, I have to create some question objects. And I'm 78 79 00:07:34,380 --> 00:07:38,960 going to create it exactly the same way as I did up here for q1. 79 80 00:07:39,000 --> 00:07:47,520 I'm going to use the question constructor. I'm going to provide a value for q, which is the first question, 80 81 00:07:48,570 --> 00:07:55,130 and then I'm going to provide a value for a, which in this case is false. 81 82 00:07:55,140 --> 00:08:03,810 So now let's go and create our next question and this one has a cue of this string. 82 83 00:08:05,040 --> 00:08:12,650 And the a should be set to true. So let's put that here. 83 84 00:08:12,770 --> 00:08:20,840 Now finally, we're on to our last question and we just have to provide our q and or a just as we did 84 85 00:08:20,840 --> 00:08:26,070 before. And this one is also going to be true. 85 86 00:08:26,110 --> 00:08:26,480 All right. 86 87 00:08:26,510 --> 00:08:35,090 So now we have all 3 question objects inside a list that contains question objects 87 88 00:08:35,090 --> 00:08:37,480 and it's called our questionBank. 88 89 00:08:37,550 --> 00:08:40,930 So we have no need for any of this anymore. 89 90 00:08:41,030 --> 00:08:48,350 And it's now very succinctly linked up our questions with our answers for every one of our questions 90 91 00:08:48,350 --> 00:08:54,800 in our question bank. So you can either delete all of this code or you can keep it around as a comment 91 92 00:08:54,920 --> 00:08:56,400 if you want to refer to it. 92 93 00:08:56,510 --> 00:08:58,390 But I'm going to get rid of it. 93 94 00:08:58,520 --> 00:09:05,660 So now we have some errors and we can tell that we have errors because Dart analysis tells us that. 94 95 00:09:05,660 --> 00:09:11,570 But also you can see at a glance on the right hand side, when you have a little red button there it tells 95 96 00:09:11,570 --> 00:09:17,900 you, you've got errors. In order to find our errors, we can see there's one here there's one here and there's 96 97 00:09:17,900 --> 00:09:18,320 one here. 97 98 00:09:18,890 --> 00:09:27,350 And if we scroll down, our first error is here. The text inside our text widget. Because previously, we 98 99 00:09:27,350 --> 00:09:33,620 had a list called questions and we pulled out the item at whichever one questionNumber is at. 99 100 00:09:33,650 --> 00:09:36,880 So at the moment, it's the first item in that list. 100 101 00:09:37,430 --> 00:09:39,830 But now questionNumber is gone, 101 102 00:09:39,860 --> 00:09:43,000 so how do we get it out of the question bank? 102 103 00:09:43,160 --> 00:09:50,440 Well it's pretty much the same way. We tap into our list called questionBank. 103 104 00:09:50,840 --> 00:09:54,830 We pull out the question at this index. 104 105 00:09:54,830 --> 00:10:00,860 So it'll be a question bank at index 0 when we first start up our app. 105 106 00:10:00,860 --> 00:10:07,690 So we now have access to this question object. But that doesn't work because our text widget needs a 106 107 00:10:07,700 --> 00:10:08,990 string. 107 108 00:10:08,990 --> 00:10:17,990 So we need to display this part which in our question class, is known as the questionText. So we can 108 109 00:10:17,990 --> 00:10:27,890 simply write question bank at this index, .questionText. And now we'll be able to do exactly the same 109 110 00:10:27,890 --> 00:10:31,500 thing but using our new question bank instead. 110 111 00:10:31,580 --> 00:10:34,910 So let's scroll down and fix the next one. 111 112 00:10:34,910 --> 00:10:41,630 The next issue is here, and we used to have our answers list but now that's gone and is replaced by our 112 113 00:10:41,630 --> 00:10:42,590 question bank. 113 114 00:10:43,160 --> 00:10:48,740 So we're going to get the correct answer by tapping into the question bank at a particular question 114 115 00:10:48,740 --> 00:10:54,380 number, and then we're going to write .questionAnswer. 115 116 00:10:54,590 --> 00:11:01,880 And that of course comes from this property right here which is set to the value of a, when we create 116 117 00:11:01,940 --> 00:11:09,340 this question object. Now that we've addressed that one, we've got one more error to go and it's right 117 118 00:11:09,340 --> 00:11:09,850 here. 118 119 00:11:09,850 --> 00:11:16,720 And it's exactly the same as before, question bank at the index of question number .questionAnswer 119 120 00:11:16,840 --> 00:11:24,670 to get the correct answer for the current question. So we fixed all our errors and if we save and go 120 121 00:11:24,670 --> 00:11:31,810 ahead and hot restart our app, then it should reset our question number back down to zero, and we should 121 122 00:11:31,810 --> 00:11:33,580 start on the first question. 122 123 00:11:34,180 --> 00:11:41,740 If we click true for this question, you can see that down here, we get user got it wrong because the first 123 124 00:11:41,800 --> 00:11:49,240 question's answer is false. And you can progress through this of course until you run out of questions 124 125 00:11:49,540 --> 00:11:51,450 at which point it'll crash out again. 125 126 00:11:51,640 --> 00:12:02,230 But now, we've organized our questions and answers together into what's called an object. And we created 126 127 00:12:02,230 --> 00:12:12,740 our objects by creating a class. So if the concept of classes and objects is unfamiliar to you and you're 127 128 00:12:12,740 --> 00:12:18,710 not quite sure what's actually going on here, then be sure to head over to the next lesson where we do 128 129 00:12:18,710 --> 00:12:24,120 a deep dive on classes and objects and how they work in Dart. 129 130 00:12:24,140 --> 00:12:31,280 Now if you're very familiar with this and you can understand how the constructor works and how our classes 130 131 00:12:31,280 --> 00:12:38,240 are created and how our objects are constructed, then feel free to skip the next lesson and continue building 131 132 00:12:38,240 --> 00:12:39,250 out our app. 15173

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.