All language subtitles for 009 Handling Dart Futures_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian Download
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:01,020 --> 00:00:05,250 In the last section, we took a look at the documentation for the jet function, and we noticed that 2 00:00:05,250 --> 00:00:09,780 any time we call get it appears to return something called a future. 3 00:00:10,200 --> 00:00:14,460 So in this section, we're going to talk a little bit about what futures are and get a better handle 4 00:00:14,460 --> 00:00:14,850 on them. 5 00:00:15,540 --> 00:00:18,240 Now, let me cut to the chase right away with futures. 6 00:00:18,600 --> 00:00:25,530 If you've ever worked with JavaScript before and you are familiar with promises, a JavaScript promise 7 00:00:25,830 --> 00:00:28,780 is identical to a future in DART. 8 00:00:29,100 --> 00:00:34,320 So when you see the word future, you can just think JavaScript promise is the same thing. 9 00:00:35,100 --> 00:00:39,370 If you've never worked with JavaScript before or you've never dealt with promises, that's totally fine. 10 00:00:39,390 --> 00:00:41,160 We'll talk about what they are right now. 11 00:00:41,910 --> 00:00:44,820 So let's first get started by taking a quick look at a diagram. 12 00:00:45,940 --> 00:00:52,570 So inside this diagram, we've got our mobile device over here that's going to make a HTTP request over 13 00:00:52,570 --> 00:00:55,700 the Internet to this outside API server. 14 00:00:56,350 --> 00:00:58,360 It's going to request some amount of data. 15 00:00:58,810 --> 00:01:02,440 The API server will see the request and respond with the data. 16 00:01:03,160 --> 00:01:08,110 Now, what this diagram right here doesn't show is the fact that it might take some very substantial 17 00:01:08,110 --> 00:01:10,690 amount of time for this request to be completed. 18 00:01:11,290 --> 00:01:16,210 Our mobile device might have a really spotty Internet connection, and it might take this request anywhere 19 00:01:16,210 --> 00:01:20,550 from one hundred milliseconds to 10 seconds to complete. 20 00:01:21,070 --> 00:01:27,250 So we have to assume that it's going to take some amount of time to fetch our data from this API. 21 00:01:27,610 --> 00:01:30,190 And that's what that future is meant to help with. 22 00:01:30,560 --> 00:01:36,580 Futures are meant to help with code that will take some amount of time to execute or any event that 23 00:01:36,580 --> 00:01:42,010 might take some amount of time to execute, to get a better handle on what a future actually is and 24 00:01:42,010 --> 00:01:43,020 how we interact with it. 25 00:01:43,330 --> 00:01:46,380 Let's work on a quick example over inside of Dart Pad. 26 00:01:47,230 --> 00:01:51,040 So I met Dart Pad, Dot Dart langue over here. 27 00:01:51,040 --> 00:01:53,670 I'm going to first write out my main function. 28 00:01:54,910 --> 00:01:58,120 I'm not going to put the void return on this just yet. 29 00:01:58,390 --> 00:01:59,590 You'll see why in just a moment. 30 00:02:00,340 --> 00:02:06,070 And then at the very top, I'm going to immediately import a package called Dart Async. 31 00:02:07,160 --> 00:02:12,560 This async module as a part of the Dart Standard Library has all the support for fut. include inside 32 00:02:12,560 --> 00:02:12,860 of it. 33 00:02:13,820 --> 00:02:16,970 Then at the bottom of this file, I'm going to create a new function. 34 00:02:17,240 --> 00:02:18,820 You do not need to write this function. 35 00:02:18,830 --> 00:02:20,730 I encourage you to just watch for a moment. 36 00:02:21,080 --> 00:02:26,570 It's going to be a function that's going to simulate how that get function from the package works. 37 00:02:26,960 --> 00:02:29,660 So I'm just going to copy and paste it over like so. 38 00:02:30,710 --> 00:02:34,580 So we don't really need to know a whole lot about the jet function right here, though, just copy paste 39 00:02:34,580 --> 00:02:41,750 it in again, it's really just supposed to kind of mock or copy how the jet function from works so we 40 00:02:41,750 --> 00:02:43,910 can call, get and pass in a string. 41 00:02:44,090 --> 00:02:48,770 And then at some point in the time, in the future, it's going to return some data to us. 42 00:02:49,650 --> 00:02:54,830 So let's get a better idea of how to work with the future that gets returned from the jet function. 43 00:02:56,440 --> 00:03:01,480 Inside of mine, I'm going to call get in, I'm going to pass in just some, you know, arbitrary, 44 00:03:01,480 --> 00:03:05,230 whatever you call the get function doesn't actually make a request. 45 00:03:05,230 --> 00:03:07,930 So I'm just pretending like we are making a request right here. 46 00:03:09,240 --> 00:03:16,560 So when we make this request, it returns back to us, a future that will resolve at some point in time 47 00:03:16,740 --> 00:03:20,430 with the actual data that we got back from this fake request. 48 00:03:21,360 --> 00:03:25,590 If we just print out the results of the jet function right here like so. 49 00:03:27,050 --> 00:03:32,830 Then we're going to see up here that we get back a future so we get instance of future right here to 50 00:03:32,840 --> 00:03:37,790 the future or whatever you get back from the return or the get function right here is not directly useful 51 00:03:37,790 --> 00:03:38,310 to us. 52 00:03:38,650 --> 00:03:44,350 Instead, this future is an object that will notify us when our data has been successfully fetched. 53 00:03:45,050 --> 00:03:50,630 So I'm going to delete that print and instead I'm going to add on up top a print statement that says 54 00:03:50,840 --> 00:03:52,760 about to fetch data. 55 00:03:54,540 --> 00:04:00,690 And then to get some notification when our data has successfully then fetched or to kind of hook into 56 00:04:00,690 --> 00:04:07,100 this future and be told when the future is resolved successfully, we can change on a dot, then function. 57 00:04:07,950 --> 00:04:12,280 So on the future that gets returned, we're adding on a dot, then function call. 58 00:04:13,170 --> 00:04:17,370 This is a function that we can call with a function like. 59 00:04:17,370 --> 00:04:23,490 So now the inner function that we are providing right here is going to be called with the result of 60 00:04:23,490 --> 00:04:24,100 the future. 61 00:04:24,570 --> 00:04:30,390 So I might receive that argument as something called result and then I will print it out inside there 62 00:04:30,420 --> 00:04:30,960 like so. 63 00:04:32,050 --> 00:04:37,660 So now if I run this code, I will instantly see about to fetch data appear, so there it is. 64 00:04:37,870 --> 00:04:44,020 And then three seconds later I will see got the data, they got the data string right here is what our 65 00:04:44,020 --> 00:04:45,110 future resolved with. 66 00:04:45,120 --> 00:04:47,770 So you can see down here, I'm returning the string, got the data. 67 00:04:48,520 --> 00:04:55,020 So essentially to get the data or to get the information that comes back from our request, we can change 68 00:04:55,030 --> 00:04:56,440 on a dot then statement. 69 00:04:56,860 --> 00:05:01,930 We can pass in a function that will eventually be called with the data that got returned from that API 70 00:05:01,930 --> 00:05:02,470 endpoint. 71 00:05:03,520 --> 00:05:09,070 Now, there's one other way of working with promises, and this other way is a lot more convenient than 72 00:05:09,070 --> 00:05:11,100 worrying about this then thing right here. 73 00:05:11,890 --> 00:05:19,030 So I'm going to delete the code I have right now and only leave behind the print statement and the get. 74 00:05:19,870 --> 00:05:25,390 So the other way of working with futures, and this is the much more easy and straightforward way of 75 00:05:25,390 --> 00:05:31,330 working with them, in my opinion, is to use a slightly different syntax called the async await syntax. 76 00:05:31,780 --> 00:05:36,010 So again, if you're coming from the JavaScript world, this is probably going to seem pretty darn familiar. 77 00:05:37,310 --> 00:05:43,700 Our get function right here returns a future we can wait for that future to be resolved automatically 78 00:05:43,730 --> 00:05:49,360 and get the data that comes back from it by adding the awaits keyword in front of it. 79 00:05:50,180 --> 00:05:55,700 Now, whatever this promise resolves with will be returned from this overall awake call. 80 00:05:56,330 --> 00:06:03,920 So I can say var result equals async first me Fasel equals a wait get blah blah blah. 81 00:06:05,200 --> 00:06:10,720 Whenever I use the awake key word right here, I have to mark the inclosing function with the async 82 00:06:10,720 --> 00:06:11,320 keyword. 83 00:06:12,190 --> 00:06:17,140 So right after my argument list up here, I'm going to add in the async keyword and now I should see 84 00:06:17,140 --> 00:06:18,010 that er go away. 85 00:06:19,250 --> 00:06:22,760 All right, going to zoom in or zoom out just for a second so we can see that whole line like so. 86 00:06:24,570 --> 00:06:29,940 So by adding in the async key word right here, it informs darte that our function contains a future 87 00:06:29,970 --> 00:06:35,370 and it needs to at some point time wait for that future to be resolved before continuing execution of 88 00:06:35,370 --> 00:06:35,940 this function. 89 00:06:36,900 --> 00:06:40,710 So the async and await keywords go together very closely. 90 00:06:41,760 --> 00:06:46,170 After a waiting for the results from this dysfunction right here, we can then freely print out the 91 00:06:46,170 --> 00:06:47,430 result like so. 92 00:06:48,400 --> 00:06:54,850 So if I now run this code, we're going to see about to fetch data and then three seconds later. 93 00:06:55,780 --> 00:06:57,230 Got the data appears as well. 94 00:06:58,000 --> 00:07:01,900 So, again, throughout the rest of this course, we're going to use the async await syntax because 95 00:07:01,900 --> 00:07:04,240 it's extremely effective and very concise. 96 00:07:04,450 --> 00:07:11,980 But just so you know, we could also as easily done the more classic dot than syntax like that as well. 97 00:07:12,370 --> 00:07:14,050 Totally up to you which way you want to go. 98 00:07:14,260 --> 00:07:18,940 You will see some documentation that uses the DOT then syntax right here, which is the only reason 99 00:07:18,940 --> 00:07:19,700 I show it to you. 100 00:07:20,140 --> 00:07:24,040 In fact, if we go back over to the HTP package documentation. 101 00:07:26,570 --> 00:07:33,920 And go to the main page right here, you'll see that they use the DOT then syntax here as well, so 102 00:07:33,920 --> 00:07:36,620 you will see the dot then syntax used in documentation. 103 00:07:36,800 --> 00:07:38,710 But for you and I will use async await. 104 00:07:39,350 --> 00:07:41,120 OK, so that's a little bit more on FUT.. 105 00:07:41,120 --> 00:07:46,790 Let's take a quick pause, come back to the next section and handle the future that we get back from 106 00:07:46,790 --> 00:07:48,220 our get request right here. 107 00:07:48,470 --> 00:07:49,780 So I'll see you in just a minute. 11118

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