Flutter Isolate | Run tasks in background using Isolates | Multithreading in Flutter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign flutter and what is the benefit of using isolate and when to use it okay so let's get started okay so as you know that that is a single threaded programming language that means it can perform only one task at a particular time as the thread is busy so here is an example I have created just to make the thread busy what will happen let's check it out so here I have created a circular progress indicator as you can see over here and there is the button so when this button is being pressed I am just calling a function that will pass this number that is 4 billion number that is 4 million to this function so in this function I am just looping I am making use of for Loop to iterate for 4 billion time and let's check out what happened to our UI that is a progress indicator once our thread is busy in uh completing this task okay so you can see when I press on this button so you can see the progress indicator gets hanged or it gets freezed for uh milliseconds you as you saw so you can see everything is getting free so you can see button press is also getting freezed over here so that means that uh until this task gets completed even our UI is getting hanged or freezed and this is not a good way to develop any flutter application at it as it will affect our user experience okay so to get rid of this or to get this result we are going to make use of isolate in flutter so that so that we can just perform heavy tasks separately uh by keeping the main main thread free okay so the main thread is running this event that is the UI part and when I when I press on this button uh the main thread will get will be free and the task that need more time will be executed separately okay so let's check out how to make use of isolate to get rid of this issue that is the freeze user okay so now let's create an isolate function that will run its task separately for keeping the main thread free okay so here ah let me create an isolate function all right so let I will just name it as isolate function okay so this function will be of type A sync will follow a sync event I think of it okay so here I've just created a function so let's call that function from this button so I will just create one more button and I will just name it as start heavy process with isolate okay so when this button has been pressed I want to just call this function so let's call it like this okay so now let's code this isolate function so in isolate function we are going to make use of a property called as isolate spam okay so this spam has been used to handle all the events that happens inside isolate and to create separate memory or separate concurrent event for this particular function okay so let's do that so here first of all we must create a receiver port so we can make use of receiver Port like this and just clear an instance of this Constructor okay and let's make use of try and catch block to handle any errors so here on object print and I will just name it as isolate world okay so whenever isolate field I need to close the receiver Port okay so this receiver Port has been used to get the messages that is coming to the isolate events okay so here in the try and catch block let's create a isolate spam okay so for this await isolate dot St awn spanner okay so to make use of this function uh we must import this diet isolate okay so it get automatically uh inserted when you type this okay so now from the span function we must call another function that will help that will actually perform a particular task so let's create a function over here so int I will just name it as run task okay so here in this function we must pass some parameter so we will pass this receiver port and how much time uh this function should iterate as we have done in this function so what I will do is I will just copy this thing what has been iterated over here I will just copy it and I will just paste it over here okay and yeah okay so from here I need to pass this receiver port and how much time this should get iterated so first of all we must call this function so like this run task we can just call and the second argument in this pan is the data that we need to send to this function that is the parameter so first of all we must send this receive receiver Port dot send port and the second parameter is how much time this Loop should get executed so I will just enter some number okay and then here we need to accept this parameter and as you can see this is a list so let's accept a list over here so the list will be of type dynamic and I will just name it as ARG arguments okay and here this Loop will execute for argument 2. so here so this argument is this is index 0 this is index one so argument of index one this much time this Loop will execute okay and now here we must make use of sender port and everything so return back the data to this function so for this sender port the result hold is equal to argument ARG of index 0. okay so from here we are going to attach this receiver port to this sender port okay so what this will do is it will help us to communicate with this isolate by making use of this port okay okay so once this task means this Loop has been completed we must return the function so you can see I must return as we are going to make use of int we must return something from this so what we will do is I will just make use of isolate dot exit and before exiting what I will do is I will just pass this result back to our function so here result Port we are going to attach our value that has been created that has been iterated so for example this must time the loop will get iterated then the value of value will be this value okay okay so now once our exit value has been completed means that this iteration has been completed this function will execute and it will just come over here so here final response is equal to a weight receiver Port DOT first okay so once this isolate spam get completed this will get executed and our receiver Port will have some data at the first parameter that is the first item from the list and we will just print it on our console okay so here we will just print the data so data processed okay so let me name it anything and here I will just print the value that has been written from here okay so now let's run this code and I'm just calling this function okay yeah okay so I think it has been running let's wait yeah so when I press on this button so you can see our UI get hanged right you can see it has been getting hanged until the process get executed okay so let me try it or yeah over here okay so now I am just pressing this without isolate function so you can see it is getting hanged and as soon as the task get completed the UI is getting work smoothly again so now let's check this if it is working or not so I have just pressed it and you can see nothing has been affected to the UI and after completing the task you can see the log has been printed so let's try again so you can see the UI is not getting affected and it is taking some time to execute and it is just processing everything smoothly so this is the benefit of making use of isolate when you are working with any function that takes some time or heavy process is been used by any function so at that time we can make use of isolate so that's all for this video tutorial thanks for watching please subscribe protocol as Point thank you
Info
Channel: Proto Coders Point
Views: 6,701
Rating: undefined out of 5
Keywords: flutter, flutter tutorial, android, android tutorial, flutter isolate, flutter isolate example, multithreading in flutter, flutter isolate tutorial, flutter multithreading, flutter threading, asynchronous programming in flutter, dart isolates, flutter compute function, background processing in flutter, flutter async computations, flutter worker threads
Id: AnhYesrwW2I
Channel Id: undefined
Length: 10min 23sec (623 seconds)
Published: Sat Jun 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.