All language subtitles for 16. Passing Data Backwards Through the Navigation Stack

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 Download
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
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:00,510 --> 00:00:07,380 In the last lesson we saw how we were able to capture the value that the user typed into the text field 2 00:00:07,830 --> 00:00:11,710 and we did that through listening to the unchanged property. 3 00:00:11,820 --> 00:00:18,720 So whenever the text field changes it triggers this anonymous callback and it passes in the new value 4 00:00:18,810 --> 00:00:21,750 which we were able to print into the console. 5 00:00:21,750 --> 00:00:26,220 But instead of printing it into the console let's store it in a property. 6 00:00:26,220 --> 00:00:33,300 So let's create a new string property called City name and it's going to start out empty. 7 00:00:33,300 --> 00:00:41,100 Now when our text field changes we're going to set the city name to equal that new value that the user 8 00:00:41,100 --> 00:00:49,590 typed into the text field and now all we have to do is to try and pass the city name back when the user 9 00:00:49,590 --> 00:00:52,020 taps on the Get whether button. 10 00:00:52,230 --> 00:01:01,410 So inside these curly braces we have to somehow pop this city screen off of the navigation stack and 11 00:01:01,410 --> 00:01:06,450 pass that city name value back to the previous screen. 12 00:01:06,450 --> 00:01:13,370 So far we've only really seen how to pass data forward passing a piece of data forwards to a state object. 13 00:01:13,410 --> 00:01:19,410 We can do that by passing it to the state full widget and then fetching it through the widget property 14 00:01:19,680 --> 00:01:20,880 in the state object. 15 00:01:21,300 --> 00:01:26,540 But now we have to figure out how to pass data backwards. 16 00:01:26,550 --> 00:01:33,810 Well let's first add aunt navigator dot pop method and I'm going to hover here on this pop method to 17 00:01:33,810 --> 00:01:35,480 show you the documentation. 18 00:01:35,730 --> 00:01:38,550 You can see that it's got two values. 19 00:01:38,580 --> 00:01:45,160 One is the context which is the current context and we always pass that in order to pop the screen off. 20 00:01:45,300 --> 00:01:50,710 But there's also a second optional input which is a result. 21 00:01:51,000 --> 00:01:55,470 And this is where we're going to add the value that we want to pass back. 22 00:01:55,470 --> 00:02:01,260 So we're going to write navigated up pop pass in our current context or where we are in the widget tree 23 00:02:01,770 --> 00:02:06,690 and then we're going to pass over that city name variable. 24 00:02:06,910 --> 00:02:12,880 Now if you had more things to pass you could pass a map for example or even an object. 25 00:02:13,060 --> 00:02:22,360 But now that we've passed this bag where can we access it in our location screen well if we take a look 26 00:02:22,480 --> 00:02:28,030 at our push method you can see that it actually has an optional output. 27 00:02:28,030 --> 00:02:37,240 It may output a future with any sort of dynamic type and that output comes from over here. 28 00:02:37,240 --> 00:02:40,450 So when we popped it we passed something over. 29 00:02:40,540 --> 00:02:46,630 Well that's going to be the output of our navigator dot push method. 30 00:02:46,630 --> 00:02:55,290 So we can tap into it by creating a variable code typed name and we can set that to equal the output 31 00:02:55,440 --> 00:02:57,870 of navigated up push. 32 00:02:57,870 --> 00:03:05,460 So once the city screen gets pushed on top of the location screen then the user types in the value for 33 00:03:05,460 --> 00:03:13,560 city name and once the screen gets popped off city name gets passed back as the output of our typed 34 00:03:13,560 --> 00:03:14,400 name. 35 00:03:14,400 --> 00:03:19,820 But remember this is of course an asynchronous method because you never know when the user is going 36 00:03:19,810 --> 00:03:21,910 to type in the city name right. 37 00:03:21,930 --> 00:03:24,670 So this is why it returns a future. 38 00:03:24,720 --> 00:03:31,710 So if we need to use this type name somewhere a little bit later on then we have to make this a wait 39 00:03:31,890 --> 00:03:39,720 for the result of this and which means that we have to add async to modify on pressed and you can add 40 00:03:39,720 --> 00:03:47,490 the keyword async to anonymous functions like this or as we've done before to named functions a method 41 00:03:47,790 --> 00:03:55,530 like so it doesn't really matter as long as you put it in front of the curly braces now that we've got 42 00:03:55,530 --> 00:04:02,790 access to this typed name variable which comes from the user input from the city screen well we can 43 00:04:02,790 --> 00:04:03,690 now use it. 44 00:04:03,720 --> 00:04:10,860 So let's confirm that we've actually got something back by printing out the typed name into the console. 45 00:04:10,890 --> 00:04:12,780 So now let's check this out. 46 00:04:12,780 --> 00:04:18,870 Let's click on hot restart so that our app starts from the beginning so that we load up the location 47 00:04:18,870 --> 00:04:19,520 screen. 48 00:04:19,650 --> 00:04:25,050 And this is where of course our navigator dot push gets created. 49 00:04:25,050 --> 00:04:31,380 So now let's navigate to our city page and here I'm going to add the name of the city that I want the 50 00:04:31,380 --> 00:04:36,540 weather for and then I'm going to click on get weather which of course takes us back to the previous 51 00:04:36,540 --> 00:04:42,760 screen and it's here where we get that typed name printed into the console. 52 00:04:43,230 --> 00:04:50,250 Now that we've got our typed name here let's use it to actually fetch the current weather. 53 00:04:50,250 --> 00:04:53,580 But let's first make sure that it's not no. 54 00:04:53,880 --> 00:05:01,230 And we can do that by adding an if statement checking to see if type name is not equal to no. 55 00:05:01,350 --> 00:05:04,780 Well in that case we're going to actually be able to use it. 56 00:05:04,990 --> 00:05:09,390 And in order to use it we're going to call a method in our weather. 57 00:05:09,390 --> 00:05:16,740 Dot dot but we can't use the get location weather method because that relies on getting the weather 58 00:05:16,830 --> 00:05:23,040 based on the current location whereas we might be nowhere near the city that the user typed in to the 59 00:05:23,040 --> 00:05:24,120 city screen. 60 00:05:24,120 --> 00:05:33,360 So we have to create a new method here and we'll create one and we'll call it get city weather but it's 61 00:05:33,360 --> 00:05:34,800 going to work very similarly. 62 00:05:34,800 --> 00:05:38,170 We're also going to be using our network helper. 63 00:05:38,430 --> 00:05:43,380 And we're going to be passing it a you are well that's going to be based off the open weather map your 64 00:05:43,390 --> 00:05:43,890 arrow. 65 00:05:44,250 --> 00:05:51,480 So let's take a look at the documentation for open weather map to see how we can get the weather data 66 00:05:51,690 --> 00:05:54,500 based on a city name. 67 00:05:54,540 --> 00:06:02,250 So the first one even is get weather by city name and you can see that this is how you would structure 68 00:06:02,370 --> 00:06:04,230 your API calls. 69 00:06:04,230 --> 00:06:08,000 So we have to add in a parameter name called Q.. 70 00:06:08,100 --> 00:06:13,680 And afterwards we set it to equal the city that we're interested in and then we also have to pass in 71 00:06:13,740 --> 00:06:16,400 the app I.D. which we've already registered. 72 00:06:16,560 --> 00:06:21,420 So let's format our U.R.L. here inside get city weather. 73 00:06:21,600 --> 00:06:29,200 Let's create a var that's called U.R.L. and it's going to be equal to the open weather map you are. 74 00:06:29,640 --> 00:06:32,130 But then we're going to add some other parts to it. 75 00:06:32,310 --> 00:06:40,290 So we're going to add a question mark and then Q And then an equal sign and afterwards is where our 76 00:06:40,290 --> 00:06:42,500 city name is going to come. 77 00:06:42,540 --> 00:06:47,810 So for our get city weather method we have to pass in the city name. 78 00:06:47,850 --> 00:06:53,130 So let's add an input for this method we'll call it city name. 79 00:06:53,310 --> 00:06:56,160 And so that we can use it to form our you are well. 80 00:06:56,190 --> 00:07:02,940 So let's add our dollar sign and put in our city name at this position and then we of course have the 81 00:07:02,940 --> 00:07:07,280 tag on other parameters such as the app I.D.. 82 00:07:07,320 --> 00:07:16,380 So let's add are Ampersand or are and symbol and add our app I.D. parameter or you can simply just copy 83 00:07:16,500 --> 00:07:18,300 this part that's in green as well. 84 00:07:18,300 --> 00:07:19,890 It's exactly the same. 85 00:07:19,890 --> 00:07:25,320 And make sure that you don't have any typos and then we're going to add our constant which is our API 86 00:07:25,320 --> 00:07:27,060 key here as well. 87 00:07:27,060 --> 00:07:33,570 And finally I still want my temperature to come back in Celsius rather than Kelvin's so I'm going to 88 00:07:33,570 --> 00:07:34,370 tag on the. 89 00:07:34,410 --> 00:07:44,160 And unit equals metric at the very end and now I can add my semicolon to finish off my U.R.L.. 90 00:07:44,380 --> 00:07:50,440 So now what I have to do is very similar to what we did for get location whether I'm going to create 91 00:07:50,500 --> 00:07:55,620 a new network helper object and we'll call it just network helper. 92 00:07:56,040 --> 00:07:58,510 And we're going to initialize it with that. 93 00:07:58,510 --> 00:08:01,070 You are all that we created right there. 94 00:08:01,150 --> 00:08:07,480 Now you can either do it like this in two lines or you can use what we did down here which is simply 95 00:08:07,510 --> 00:08:12,130 inserting this line of code into right here. 96 00:08:12,130 --> 00:08:13,450 Both ways work. 97 00:08:13,450 --> 00:08:15,590 It all depends on which way you find. 98 00:08:15,610 --> 00:08:18,450 Easier to read and easier to understand. 99 00:08:18,440 --> 00:08:24,730 Now once we've initialized on network helper with the U.R.L. that we want to get data from then we can 100 00:08:24,730 --> 00:08:32,560 call our network helper dot get data method and we can save the result of that into a new variable called 101 00:08:32,860 --> 00:08:34,610 weather data. 102 00:08:34,690 --> 00:08:36,610 So so far very similar. 103 00:08:37,240 --> 00:08:45,530 And finally we can return our weather data as the output of our get city weather method. 104 00:08:45,670 --> 00:08:52,570 And of course that means we have to change this into a dynamic output but not just any dynamic output 105 00:08:52,600 --> 00:08:59,780 it's going to be a future dynamic output again exactly the same as before. 106 00:08:59,950 --> 00:09:07,960 And now we have to mark this method as an async method because get data is asynchronous. 107 00:09:07,960 --> 00:09:11,890 So in order to return it we have to wait on this to finish. 108 00:09:11,890 --> 00:09:19,660 So let's add the await keyword here and that's all we need to do to set up or get city weather method 109 00:09:19,870 --> 00:09:25,070 to be able to fetch the weather data based off a city name that we pass in. 110 00:09:25,690 --> 00:09:32,260 So now let's head back to our location screen and inside this part where we've already checked to make 111 00:09:32,260 --> 00:09:35,090 sure that type name definitely has a value. 112 00:09:35,110 --> 00:09:43,390 Well now we can pass over typed name to our weather model which remember resides in a object called 113 00:09:43,450 --> 00:09:44,000 weather. 114 00:09:44,140 --> 00:09:50,020 But the data type is our weather model which comes from the weather dot dot and we're going to tap into 115 00:09:50,020 --> 00:09:56,980 that method we created just now which is get city weather and we're going to pass in the city name by 116 00:09:57,040 --> 00:10:05,110 using that typed name that we got back from our text field once we've gotten the weather data back then 117 00:10:05,170 --> 00:10:13,720 we're going to save it inside a variable which will call weather data and because this is an asynchronous 118 00:10:13,720 --> 00:10:21,640 method remember it returns a future then we should also mark this with an A Wait keyword because the 119 00:10:21,640 --> 00:10:29,260 very next step we're going to pass this weather data to update you method so that we can update the 120 00:10:29,260 --> 00:10:32,110 user interface of our location screen 121 00:10:34,880 --> 00:10:37,250 and then that will go inside here. 122 00:10:37,250 --> 00:10:43,870 And as long as weather data is not equal to null then it will try to get the values of our temperature 123 00:10:43,880 --> 00:10:48,440 weather icon weather message and city name from it. 124 00:10:48,440 --> 00:10:56,930 So now let's stop our app and run it from cold and now let's head over to the city screen and enter 125 00:10:57,020 --> 00:10:58,220 a new city name. 126 00:10:58,250 --> 00:11:06,140 Let's try Beijing maybe and click on get weather so it pops off the city screen and it takes us back 127 00:11:06,140 --> 00:11:11,970 to the location screen and it tells us the weather currently in Beijing. 128 00:11:11,990 --> 00:11:17,780 Now we can of course still click on our location icon to get the weather for our current location but 129 00:11:17,780 --> 00:11:21,980 we can also change the city to any place that we can think of. 130 00:11:21,980 --> 00:11:25,910 Let's see what it's like in Paris at the moment cool. 131 00:11:26,270 --> 00:11:31,940 So the final thing that we just need to add is this button currently doesn't do anything yet. 132 00:11:32,060 --> 00:11:38,330 And what it should be is more like a cancel button just so that when we get over to this screen and 133 00:11:38,330 --> 00:11:41,980 we decide that actually I don't really know which city I want the weather for. 134 00:11:42,350 --> 00:11:45,400 We should be able to head back and cancel the action. 135 00:11:45,560 --> 00:11:52,430 So all that button needs to do is to pop the screen off without passing anything over so let's head 136 00:11:52,430 --> 00:11:59,570 over to where that button lives which is on the city screen and it's right here in the flat button. 137 00:11:59,570 --> 00:12:06,830 So at the moment on pressed is empty but we're going to change it to navigator dot pop and we're just 138 00:12:06,830 --> 00:12:10,040 going to pass over the context and nothing else. 139 00:12:10,040 --> 00:12:18,440 Remember that if at this point if we didn't check if type name is equal to no then we could potentially 140 00:12:18,440 --> 00:12:25,670 get into some problems by trying to get the weather on a no value but because we're making that check 141 00:12:25,790 --> 00:12:28,390 then hopefully we shouldn't be affected. 142 00:12:28,430 --> 00:12:36,210 So let's run up again and let's check it so now if I click on the back button it takes me back here 143 00:12:36,270 --> 00:12:38,030 without changing the city. 144 00:12:38,250 --> 00:12:42,000 And if I click on this button it gets me the location data. 145 00:12:42,000 --> 00:12:48,930 If I click on here it gets me the city data and my app is now working beautifully. 146 00:12:48,990 --> 00:12:51,930 So I hope you enjoyed making this weather app with me. 147 00:12:51,930 --> 00:12:55,840 And there's definitely a lot of improvements that you could make to this app. 148 00:12:56,010 --> 00:13:01,640 For example instead of just getting the current weather for all location what about getting some forecasted 149 00:13:01,650 --> 00:13:02,250 weather. 150 00:13:02,250 --> 00:13:08,820 What about adding more widgets display more values on screen and you can check out all the cool things 151 00:13:08,820 --> 00:13:15,660 that you can do with the open weather map APIs such as getting an hourly forecast or some historical 152 00:13:15,660 --> 00:13:20,060 data or even things such as you the index and air pollution. 153 00:13:20,190 --> 00:13:26,130 So the world really is your oyster and I'll leave it up to you to customize and update your weather 154 00:13:26,130 --> 00:13:27,890 app if you want to. 155 00:13:28,050 --> 00:13:31,910 But that's all from me for this module and I look forward to seeing you on the next one. 17069

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