Dart Isolates - Flutter Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
oh gross much better all right let's get into it so today's topic is going to be isolates isolates are sort of a complex topic but they're pretty important if you have a complex app we're going to explain a little bit about how they work and then show you an actual example with some code so let's get into it all dart code runs in an isolate that's why they're so important because our whole program and everything that goes on within our a dart application runs within an isolate so what exactly is an isolate one isolate contains memory and an event loop and that's where all the code runs so it stores things in the memory and then whenever an event happens it obviously goes into the event loop the event loop is just a thread that executes the events that happen within our app if you're coming from java or c plus you might know that multiple threads can use the same memory in those languages in dart it's completely different isolates cannot share memory between each other so you can think of an iso as its own little box that executes logic in its own thread so then since it's all separate you can easily have more than one isolate within your app and that's how you can have multi-threaded applications using dart but just because you can have multiple isolates running doesn't mean you should for the most part most apps you only need one event loop and one isolate running all of your code the only time it's really necessary to have multiple is when you have very complex logic that will take up time and stop frames from being rendered in the main loop so my recommendation is unless your app really needs it stick with one isolate and only create a second isolate if you really really need it so there's two ways to create a new isolate the first way is using isolate dot spawn this just basically creates a new isolate and then you have actions you can perform on that isolate like kill and many more the second option is using the compute function although both options can pretty much do the same thing the compute option seems like the better way to do some hard logic that takes a lot of time now if you remember we mentioned at the beginning that the each of these isolates do not have access to each other's memory but obviously there are situations where you would need to have access to the data that that's in the other isolate and vice versa so there is a solution for this you can create a send port and a receive port these are pretty much streams where you can send data between the two isolates and you can set up a send port and a receive port on each side so you can send data between them as much as you want as you can see these isolates are very isolated and that's where they actually get the name for isolate all right so let's jump into the code and show you how isolates work within a flutter app alright so here we have some really simple code all we have in here is a simple add button there's no isolation here just a stateful widget with text and a raise button that increases a count so now let's add an isolate to this so we are going to run our ice lift from the init state function like we went over in the written part you need to do isolt dot spawn you will see it will import a dart isolate dependency and then here we need to define two variables one is the actual function we want to run and the other one is the parameters we want to pass to it so the function needs to be declared out of any class since the main isolate that runs the whole app is running these classes you need to create the function outside of those classes so it's not associated with that main isolate so we're going to call our function isolate function and we're going to just pass it a number and we can go back to this isolate spawn call the isolate function we'll give it a value of 1000. and we're going to do something really simple and have a local count that goes through a for loop and increments until the number that we passed to the isolate function if our count is divisible by 100 we'll just create a print statement just to show you that it actually works so now we refresh we should see a bunch of print statements pop out so how do we know that it's actually in a different isolate well it's pretty easy to prove it to you if we create a break point here you refresh you'll see this breakpoint will execute so our isolate is paused that means it's not running and we're able to still add everything in our normal app notice if we create a breakpoint here or is able to add it once and then nothing works anymore because programs paused but as long as our isolates pause that's no problem and if you're using vs code you will actually see on the bottom left side you have your main isolate running and then you have our isolate function paused so if we run it you'll see 100 will pop out we can run it again 200 and we can even put a break point here if you want to see increment by one it just printed the last one but you'll see the value i equals 300 i equals 301 302 and our isolate function is executing in a completely different isolate or thread so now you can do very heavy and difficult computations within the other isolate and your main app will keep running and not be worried about any of the information that's going on in there so now let's create something using the compute function so we'll create a second button called add an isolate and we'll have a function called run compute that will return us a future now for our compute function we also need to define it outside our function will be very similar to the isolate function except one little difference it will actually return an integer from it in this case we'll just return the count which is going to be the same count that we pass in it's not really logic that makes sense but you can see how this can be used for more complex logic so now we're on our run compute function we can set the state of count to a wait for our compute function to finish you define it the same way as we did with the spawn isolate and then we can call set state after this and now we should be able to get some logic going on in the background that will affect our app so now we have an isolate that will come do some logic that will get reflected into our app so we can actually set a breakpoint here to see make sure it works again and then we can use the rest of the app without it being finished we click add an isolate you see our breakpoint will hit but we can still add values within the app we're not worried about that at all then if we remove it you will see all the print statements pop out at the bottom and you'll see our value update to 2000 here just like that so that's a quick explanation on isolates this topic goes a lot deeper so you can do some research on your own and learn about how you control the isolates differently how you can use senport and receive port because we didn't cover those but we covered the basics and what an isolate actually is and how it works so once again i want to reiterate you don't really need to use ice list unless you're doing very heavy computation for example this app doesn't make any sense to use isolates we're just counting numbers and we only count it to 000 maybe if we count it to 1 million it might be worth it but even then flutter and dart is already pretty fast itself but you shouldn't need to use isolates that often and if you do then hopefully i was able to help you understand them a little bit today so this code will be on github link is in the description if you have any questions or anything make sure you leave in the comments make sure to like subscribe and share if you enjoyed the video and thanks for [Music] [Applause] [Music] watching [Music] you
Info
Channel: Tadas Petra
Views: 9,780
Rating: undefined out of 5
Keywords: coder, amateur coder, amateur, flutter, flutter tutorial, programming, coding, flutter programming, dart isolates, flutter isolates, isolates, thread, dart threads, java threads, c++ threads, flutter threads, flutter multithreading, does flutter support multi-threading, does dart support multi threading, difference between threads and isolates, what is an isolate, how to use isolates
Id: TF8LwonwKhg
Channel Id: undefined
Length: 7min 46sec (466 seconds)
Published: Fri Nov 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.