Sunday, May 22, 2022

What’s the difference between async and async* in Dart | Flutter

Hello everyone assalamualaikum here we'll take a look at the difference between async and async star so first understand what is async well async is used for long running task so async gives you a future for example like this where you have to wait one second and then return or like this where you have to make a .

Restful api request and wait for the request to be completed and then return the data so that's the future and that's async work together so async gives you a future and what's a sync star well async star gives you a stream and what is stream well stream holds futures actually stream holds a lot of .

Futures so we can understand stream holds a list of features so here's the basic understanding well essence gives you a future and essence star gives you a lot of features so let's go ahead and see the difference in example so here i am i have this main function and right below main function .

I'll write another method that's called do some long task and then i will call this one from here so i'm calling do some long task from my main function and then um this function is returning a future which is an integer value see that here i'm tagging it as a future which means i .

Have to wait and then after waiting i'll return a value which is integer so i'm doing it here both i'm saying i'm returning so i have to wait and i'm returning integer so let's go ahead and run it so here one second later we see 42 now we'll see how to use async star so here i have another function .

Now you see this function inside has a loop now inside loop we are returning a future okay but over here instead of using the return keyword we are using yield keyword why because return keywords finish the function and in general it would never execute again but here we can't finish it because .

Every one second later i have to call this so this function over here would get automatically called from here every one second later like this so like this from here this function count for one minute which is this one would get called every one second later so over here because we have to return this value every one second later from the .

Slope so over here instead of using return keyword we have to use yield keyword and see how we used async star over here so over here what we are doing we are returning a lot of features from a loop one by one after certain time later so this is also a future which returns the future okay and as i said early stream returns future so that's .

Why i have here stream because we are returning a lot of future so instead of one feature i'm writing here stream which will return a lot of future and integer value doesn't change so let's go ahead and run it so now here also 42 1 2 3 4 five six .

Well so it will continue until it counts to 60. okay so that's that's the basic difference between a sink and passing star once again passing star is combination of a lot of futures and that's all now if you really and really want to understand how async start works and where you should use it so go ahead and check out my blog .

Tutorial below where we have used this essence stir and this yield keyword to generate uh states from events in block state management package anyway so if you learn something so don't forget to subscribe and smash that like button thank you

RELATED ARTICLES

Most Popular