Isolates in Flutter | Dart Isolate Tutorial - Run tasks in background using Isolates | Multitasking

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] just like threads what we have in Java we have isolates here in flutter typically each isolate has a separate memory where it performs the even looping or in other words in order to perform multi-threading here in pratter we go for using ice rates so with the help of the isolates we'll be able to run tasks separately which is independent of the main isolator which is the darts thread and our fat wrap can have one or more isolates in such cases if there are two isolates which is running independently in our filter app and there is a data which needs to be shared between these two isolates then there comes the concept called ports and these ports are important Concept in case of isolates basically we go for using ports in order to transfer data between isolates say for example we have a sad data which has been used by two independent isolates and if these two isolates needs to communicate based upon the sad data then we go for using ports here so we basically have two ports one is the send port and the other is the receiver Port so basically when you hear the term isolate the first question which comes to our mind is why should we go for using isolates in order to answer that question we have a simple example right over here where we have a circular progress indicator which keeps loading infinitely after which we have few spacing followed by which we have the elevated button and consider this elevator button is going to perform some heavy duty task like a file operation or can be Network call it's based upon the use case of your application but consider upon clicking this elevated button it is going to perform a heavy task right and let's First Command this use isolate method and try to call run heavy task without isolate therefore whenever this elevated button is pressed we are going to call this an heavy task without isolate method and passing the argument as 4 billion and here inside this method we have a simple for Loop which is going to perform 4 billion iterations which is typically a heavy task for the Dart engine to perform so just to keep this tutorial simple we made use of this for Loop in order to mimic the heavy task okay and this is going to iterate for 4 billion and after the iteration is completed we try to print the value here the value is nothing but the value which is been added over each iteration all right consider this is a simple example where you say the on press event we try to call an heavy task without isolate here we are not going to use any isolation this is a basic flutter example where we have the for Loop which is going to perform 4 billion iterations all right so if we try to refresh this app and if you pay attention you will notice that upon clicking this button you see the circular progress indicator just stops and after this iteration is completed it receives back to its original state I can also run this example again so just clear the debug log and if I click this button the circular browse indicator freezes until this four billion iteration is complete after which the UI gets refreshed back to its original state which means that since Dart coding is single threaded the main isolate is busy executing this four billion operation where it has no time to refresh the UI that is the reason we see this UI getting freezed till this execution is complete only after this execution which is the heavy task is completed the dot isolate turns its focus to the UI to refresh it back to its original state all right so this is typical example where the UI gets stuck where the dart code is involved in performing heavy task and in order to overcome this scenario we go for using isolates I will also show you a working example now instead of calling this run heavy task without isolate method we try to call this use isolate method this use isolate method as the name suggested is going to use isolate in order to perform this heavy duty task and it calls the Run heavy task with isolate method and pass in the same count which is the 4 billion and here inside the Run heavy task with isolate method we have the same set of for Loop where the argument is going to be the same for Boolean iterations so in this scenario if I restart this application and this time we are using the isolate in order to perform the heavy task and let's see if our Federal app is getting stuck at any point of time all right so let's restart this app and if I click this button you see that the loading indicator is no way getting affected due to the heavy task and we also get the result of the heavy task which is performed independently of the main thread which is the main isolate here so this typically answers the question why we need to go for isolate so if you are performing a heavy task it is better to hand it over to an isolate where it is going to perform the task independently of the main thread by the main thread on the other hand can be involved in updating the states or the UI components so being a simple example app due to the heavy task our loading indicator gets stuck but consider large scale application where there are multiple components which is scattered around in this page each performing in individual task like a network operation a UI updation so on and so forth and at that case if you are performing heavy duty task in a single threaded operation then that would be really a worse nightmare for any developer you can also create a flutter app without isolate you can go for publishing it Play Store it is going to anyway work but in order to enhance the performance of refractor app you will go for isolates all right hope you got a better understanding of what isolate is and why do we need to go for isolates with this idea and without any further delay let's jump into the coding part and let's try to implement this simple feature with the help of the isolates in our Federal app here starting with the main.file we have a home pointing to my home page here the my home page is nothing but a stateless widget class with an empty scaffold first let's try to build up the UI components here when you see the scaffold let's first try to have an app bar after which we have a body that is going to contain a list of flutter widgets in the form of columns and the first style for the column is going to be the circular progress indicator followed by few spacing we try to define the elevated button and on the on press event of the elevated button we try to call the use isolate method which we will be defining shortly and this use isolate method is going to be an asynchronous function and inside this method we try to create an instance of a receiver port with the help of which we will be able to pass the data to one four between the isolates followed by which we try to create a try Block in order to handle any exception that is happening in case of working with isolates and inside the dry block we try to make use of this fun method and also remember we need to make use of the update here and the spawn method is going to take few arguments the first is going to be the method which needs to run inside this isolate and the method is going to be the Run heavy task with isolate method we will also write it down shortly and always make sure this method should be outside the main class because the main class by itself is a nice rate which is the main isolate here which is the dark thread therefore make sure if you define this method always place it outside the class followed by which we can also pass other arguments here I am going to pass a list of arguments and the first argument on the list is going to be the receive Port which we have created here right with the help of the dot indicator we can pass the send Port which is available in the receive board followed by which we try to specify the iterations here as mentioned we are going to make use of the for Loop that is going to take 4 billion iterations so that count is being passed as a parameter over here in say this span method and after the tray block if the isolate fails to identify the entry point then in that case we try to print isolate as failed in the debug console I will try to close the receiver port and definitely if the isolate completes executing the heavy task in order to print the value we make use of the receive Port DOT first and try to print that response here in the debug console and that's basically what we need to do in order to create an isolate now let's try to create this run heavy task method which is nothing but a simple for Loop so here inside this running heavy task method we try to pass a list of arguments and the arguments are nothing but the receive report which we have created here as well as the um iteration count and inside this run heavy task method we try to create a result Port which is nothing but the receive Port which we have passed is the parameter and in order to access that we can make use of square bracket and pass in the 0 over here which is the first argument and which is then initialized over to the send port and we create a separate variable here which is the value and inside the for Loop in order to access the second argument you pass the argument of one which is in the index one all right and each iteration the value is going to be added up and finally we try to make use of the isolated exit method and pass in the result Port which is nothing but the send Port which we have passed as the parameter over to this method and also the value so this method is going to kill the isolate and upon which we try to pass the final value which we have added up as a cumulative of the 4 billion iterations so this is basically how you can create and isolate and pass in the data 214 with the help of the sentence receive ports and as a quick summary inside this elevator button we try to call the use isolate method wherein we create an instance for the receive port and we try to make use of the isolator spawn pass in the method which is going to perform the heavy task and it also accepts arguments it can be a single argument or it can be a list of arguments in this case I have passed a list of arguments here upon completion of the isolate we try to print the response here and here we say this heavy task method we try to grab the send port and perform the heavy task and finally upon completion of the task we try to exit the isolate with the help of the dot exit method and pass in the result Port which is the send Port here and the final value which which we have applied as a result of this heavy task all right and let's refresh this app and let's try running it I click this you see behind the scene the isolate is running independently the main thread and it is completed executing and the UI is no way getting affected due to this heavy task so this is just a basic example of how isolate works here in this tutorial I have made use of a single isolate but you can extend this simple example to a large scale application where you can have more than one isolate inside a same application and pass the data to one for with the help of the ports hope you found this tutorial useful if you do so consider subscribing and I will see you again in the next video thank you [Music]
Info
Channel: vijaycreations
Views: 9,036
Rating: undefined out of 5
Keywords: flutter, dart, isolates, dart isolates, isolates in flutter, threads, threads in flutter, multitasking, multithreading, isolates example, isolates in flutter app, app development, how to use isolates in flutter, isloates in flutter app, isolates and event looping, async, await, futures, isolates in flutter apps, how to use isolates in flutter app, isolates in flutter tutorial, isloates in flutter example, working with isolates, Isolates in Flutter, Isolates in flutter, flutter isolates
Id: g6sPAWCFgtE
Channel Id: undefined
Length: 10min 40sec (640 seconds)
Published: Fri May 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.