 Hey everybody, this is Brian. Welcome to the 14th Flutter tutorial. We are getting into the advanced Dart topics Remember, we have to learn Dart before we can really dive into Flutter Today we're gonna talk about Asynchrony support or asynchronous programming. What in the heck is this? That is even a mouthful to say Hmm. I'm sure everybody listening to this has had somebody tell them Hey do this but while you're doing that also do this and then maybe do this this this this this It's really multitasking Asynchronous programming is different than multi-threaded programming under the hood on how the computer actually works with the code Won't get too deep into that how that works, but just understand that Asynchronous programming allows your code to run Asynchronously meaning at the same time as your other code and you can do some really cool stuff with this so Let's just dive right in here. Let's create a new project And next now, let's call this multi tasking May help if I pull my keyboard out a little bit here And honestly, I wasn't really gonna cover this because I'm kind of chomping at the bit to actually get into Flutter but it is a part of Dart and it's going to be heavily used a lot of the IO functions work with futures and streams objects and in the words of Ripley from the movie Aliens You started this show me everything. So if you haven't seen that movie, it's like one of the best movies of all time Anyway, so let's just dive right in here Now if you've never worked with asynchronous programming reading the official Dart documentation can be a little bit confusing And not really for the faint of heart Although it's actually broken down very well. It's just a very complex Topic and it's in a very advanced topic and even seasoned programmers make some very bad mistakes So I apologize in advance if this tutorial has some mistakes, and I'm sure everybody on YouTube will point them out I have to chuckle when I say that because they do point them out. All right, so So first thing we need to do is we need to import Dart Async and you notice how that has Dart colon async. It's in the past We've done just a raw import where you import a file and we've imported Libraries and stuff but Dart whenever you see that Dart colon that means it's part of the core framework If you will or the Dart libraries now, I should note that everything in Dart is a library even this program We're working on now is a library That is how you import something directly out of Dart. So We have to Kind of understand some things before we can really dive in here So I'm going to start with a very simple example and we're going to call this future And notice how that took a generic type, which we just covered in a previous tutorial We'll call this the long wait. All right. So already we notice us some big differences here from what we've worked on in the past Let's just finish this out real quick and I equals zero I plus then let's just say five and then I plus plus So we're gonna increment that And then we're going to just print out The prefix and the current number and at the very end of this We're going to just return true Some things you need to understand what's going on under the hood a you cannot use async without this import You will get a glaring mistake here. Let's just cut that see how suddenly Undefined class future and if I take that out that'll start lighting up And so first step is you've got to import the async library The second step is that your functions and methods will have to be treated a little bit different I keep saying functions, but they're actually called methods They're treated a little bit differently. You won't actually return a boolean You're going to return what's called a future that wraps around the boolean value that you're returning Now, what does that really mean? you have to do that because of the way the code is actually executed and Let's just do this Let's say I did print whoops Nothing too stansy about this. It looks like it'll just run, right? So let's actually Grab this and run it and see what happens here Notice how it says starting done then it says zero one two three four now wait a minute If we look at the code it says print do this which is actually this guy right here Then print done, but if you notice it's saying starting done. So it's going from here Here and skipping this guy all together. It's not actually skipping it. It's doing it in parallel This just takes longer to execute than this print statement does so that's why it says starting done But it's actually happening asynchronously aka pretty much at the same time now as You can imagine that can lead to some very big problems if you're expecting things to be in a certain order We're gonna cover how to do that, but I wanted you to understand that We're not actually getting the boolean value. We're getting a future meaning We're going to return a boolean value or whatever we put into this generic template here So to get that boolean value you have to say bull Rats whatever we want to make it Equal and we're gonna use the await keyword. So what this will do is it will say hey This isn't a sync function. You have to be in a sync function to call in a sync function As crazy as that sounds. It just needs to know. Let's actually test that Yeah, see suddenly a wait lights up and says it not gonna run So you've got to be in a sync function And the await keyword will actually well pretty self-explanatory it will await this to be completed So it's gonna say starting it's going to await for that to be completed once that's done It's going to unwrap that future object is gonna get that little value there and shove it into this one right here shove it right into that variable and then hand it off to our program and then it'll say done We're actually not really using this, but I kind of wanted to show you how to get the variable back out of there So let's we can actually let's just print this out just so that you can see there is an Actual value and not a future object See now you can see how it says starting and then we've got Zero through four and then the boolean value and then we're done So that is how you would work with that Now there is a bit of an easier way I Say easier some people love it. Some people hate it. It's just personal taste really Void and we're gonna say test them Man this keyboard's really ticking me off here Yes, in case you're wondering it's the same keyboard. I was having problems with a few weeks ago All right, so we're gonna go starting and through the magic of copy and paste here Pretty much the same structure. We are not using the async keyword here So we're in what's called synchronous code and we want to run asynchronous code Well, we have two options. We either turn our little function into an asynchronous function like we've done before or What we can do is we want to say then Get in there and this will take a little bit of explaining and this is why some people love this and some people hate this Let's actually shove this in here and see what happens. All right, so when we run this You can say it says starting done. So we think that we've already screwed up But then it says zero one two three four done waiting. I Left this done in here so that you could see what would happen. It will actually treat this actually this as Synchronous code meaning it's going to do the then Now I understand that makes no sense. What does it mean by do the then? Code execution starts here says do this long wait it goes up evaluates this and says, oh, it's an async function Good, I can just run this in parallel. So it's going to a synchronously run that while that's running It's going to jump down here and go okay. We're back into synchronous code So start done and then this executes off to the side at the same time That is where the then method comes in What then does is it says do the asynchronous code then when you're done run this code That gets a little confusing. Some people love that. Some people hate it Personally, I prefer this method because it's very clear to see what's going on however You can do some really cool things with this. So let's actually do this And we're going to just say, oh, let's just grab this whole thing rather than type all this out I'm getting old and cranky here. So all right. So now what we've got here is Let's actually do this a little differently So we're gonna grab the future object out here. Notice how we're not grabbing a bully We're grabbing the future object and from here. We are just going to say Let's run this and just make sure this works as expected before we really dive into it All right, so it's starting done and then we've got chains blah blah blah So what we're going to do here and this is where I always screw up and that's why I wanted to Kind of do this and we're gonna grab this and we're gonna say bang bang done Chain one chain to chain three So let's run this again. See what happens here now. You can see how you can actually say hey Once you're done do chain one do chain two to chain three So you can do things in a specific order after these synchronous code executes. So this could be like Calculate the Sun Moon and stars and then put it into a database and then log it and then dial into a server and spit out Some value or something so you can do what's called chaining and that's why I said chains Let's just get rid of chain here and just say done one done two done three Done done done. Anyways, that was lame If I edited my videos, I would edit that right out of the video. That was really lame so now what we're going to do is we're going to actually Test waiting on multiple asynchronous calls and this is pretty advanced and this can get kind of nuts But the syntax for it is just dead simple. That's why I'm gonna add this in here And notice how we are back to the async And we're going to say print and we're just gonna say a future F1 equal I'm gonna just copy and paste this a few times here. So we've got our Three asynchronous calls here. Let's actually comment this out so we can see it work in real time And you see how there it goes one two three That is how that's going now This does not necessarily guarantee that it's going to be in order. It may not be one two three These are each Independent distinct calls so it may be two three one or three one two or whatever permutation here So what we want to do is we want to know when all three of those are actually completed You see how we got starting done. We want done way down at the bottom here So what we have to do is we have to say a wait future and Then future itself has a wait call in it and that's how it wants multiple futures So we're just going to you guessed it. We're going to wait for those and once they're done It'll print it out Bang so starting and then there's one two three and then done That's pretty neat So just be aware that there is a lot of code Especially IO code that is synchronous and you'll have to really learn how to work with that, but it's really not that hard To be brutally honest some other languages the syntax engine like JavaScript for example is very similar But for whatever reason JavaScript makes me want to just rip the keyboard out of the wall and throw it across the room I don't know why dart. However, it's just it's very streamlined and very elegant. I really like the way it works so that in nutshell is a Synchronous programming it is by no means the lexicon of a synchronous programming This would be just like a high-level primer just so we can start working with flutter You will run into issues and you may have spotted one or two bugs in this little program here So if you find the bugs definitely go out to get hub modify the source commit it And I will approve it and we'll go on from there Speaking of if you found this educational entertaining visit my website void realms comm for the source code for this and all other tutorials and I keep saying it there is a void realms Facebook group with 1700 other programmers out there all walks of life every language you can imagine. I'm constantly asking questions out there It's a great resource for other programmers That's it. Thank you for watching