Flutter Live Learning 🤔 - Background Tasks in Flutter - Just Chatting

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so our stream is running hello everyone nice to see you around let's just wait a second s'more are just setting up bit more and then we can start for today or for the second stream of today hey Amish welcome or image welcome back I hope the sound is decent time better as you can see I have my bottle back with my nice little microphone on it I can't have put it also bit higher so hey everyone 7 people nice to see you around so let's see if I can have a good session today hey Matt go of Tom nice to see you so great ok that's much better right I hear the sound of the from the stream this morning and I was like oh geez that sounds horrible and then the drilling with it it was even worse yeah the rocket is now here I think that will be part of it till we have a stand a correct one and maybe a better microphone and everything so it will take some time I think ok so what's the plan for the second session today um I would like to take a look because I think it which is someone asked me to create or have today I think it was let me check if I find it right now where did I ask for the stream here so I can share it with you so let me switch right now and as you can see here yeah right dick emoji is also there so everywhere are emojis so I got from a bath bath but I think I got the ask for background services in flutter and notifications in firebase is another topic that I will maybe take in the future but I think background services sounds like a real big deal and that could be something that we really would to be interested in the future so I thought let's have a look into them and for that we have to know a bit more about the flutter and dart as in Cronus work right because background tasks has a special meaning and what I take want to do today is to learn it that means whenever you feel a bit bored I don't know it either so I am NOT an expert for background tasks or anything I just want to learn it today so it could be it that you are maybe a bit bored or something so if you have any ideas or if I missed anything or anything like that please let me know okay so what will be our main tool today is the browser because we will search a lot so we need Google and on the other hand I have created just if we need one a basic scaffold nothing major just the empty project at the end and I think I will even delete all of that already just to make it clear and empty have you added similar not not today I was a bit there was also a bit of other things that I needed to do today so I was a bit blocked but there will be more until the next stream I hope so maybe till Wednesday or to the Saturday I can maybe add some more stuff so what I will do is just creating a home screen on that we will work today so stateless widget cool so could you please show random text random text what do you mean with random text if you need random text I highly recommend you the website lorem ipsum because it creates you text that is really random and you have to benefit from that that you can get these top parts and you can put them in your application and it creates your paragraphs where it's and all of that stuff pretty good hey amar I was fast forwarding so I need to start Firefox again because I closed it ok mmm so how do we start if we search something like background tasks with each help in text will change according to set string values it was not the plan but it should be pretty easy to be honest you could do it in the unit state that you just create something so you if you have for example the stateless widget here but you want it on that's right image but for Android user you could just do that in your main app right so when this one is created or in the main function you create a random string here like string random and what I will do now is just taking a random number next int for example 500 to string and this is of course a string right it's getting generated all the time you can do that also in an init state or somewhere else and then you just pass it down into dear into your app that's it so whatever happens in the main function happens before your app starts so that should be pretty easy okay but back to our background tasks if we search for flutter background tasks I was there already today and that is the flutter dot def package and we don't get so much to be honest I was a bit surprised how less documented it is because what here is is that they talk about that we need something like an isolate or better we will need an isolate that we can use with the flutter comb compute is it I think so yeah parse Jason in background is maybe the better one and there we find some interesting stuff but if you talk about an isolate yes move this work to a separate isolate so as you can see we get the function from we get the front from flutter already that calls compute and it will do stuff in these background tasks are you giving to show something with hive in the future hive what is hive let's see you know not not yet hi flukes like a database or something or ya know SQL database not at the moment not planned so can the background tasks beep used for push notification yes something like that I think background processes can be used for all certain stuff so this is everything that is not in your active in your active application at the moment so if you have an emulator open you see your application and you make that small then still your app is running right and you want to have a background task this is all the time doing something so for example a timer or a counter or I think I started with the idea of a pedo trigger to check when you book and then every step that you take you get the information to your phone to your background pulses which then reads the information and displays that at the end or something like that so this would be a typical use case from can we use alarm without using native code hmm that is a task for pop top deaf if you want to do something like that then we have the alarm and you find Android alarm and I'm pretty sure there will be also somewhere in iOS alarm I could imagine as Devon I Islam hey everyone yeah so I think it said with I don't know if this is well you have to make the receivers that make sense of course but everything else is also in an isolate or very interesting it's also working with isolates cool so yes so they work with isolates and I was very interested that the documentation for this whole topic is pretty much very small yeah because I know that dart is single threaded actually and isolates are like own complete instances of the dart VM so it runs in parallel besides of each other I can also do more in in-app purchases maybe I will take a look at that next time and then you will set up your isolates for background execution and I think I can do that even better with the in-app purchases what we could do is for my I have at the moment an Android app that isn't better maybe we can do something for that then we can work also in a real-world example of an application that I developed the first time in my life so it will flutter so don't expect perfect code but we can work with that okay but the documentation is pretty slim but what we see is everything is telling us that we should use an isolate and isolate is a model for dart that to make multi-threading possible because it is not like in Java or C++ where we just see sharp where we just say okay hey I want to have a new thread and let's go we have really always these isolates and an isolate is really a can capsule of dart that is running in its own with its own memory location with its own instance and everything the disadvantage of that is we can't do everything so very easily to run more or less in parallel because we get of course like limitation in memory location and so on but benefit is that even though that it is a single-threaded technology it will get the possibility to have multiple threads work of developers to convert slim to fat okay if you say so cool and now here in that part they explained us that they don't create more documentation but there is a medium article about how you can execute dart in the background for Gayo Gayo fencing and I started to read all of that and I was a bit confused and a bit yeah it was a bit hard to read because all of these gale fencing and of course I understand somehow geofencing is a cool topic but to run something in background it doesn't really tells you how to do it because actually this shouldn't be too hard right I like the idea how they explain this part here so we have to dart isolate we have to background isolate and the dart UI isolate is talking with something in between and then the background isolate is getting triggered by that and that was something that I really liked but it was like I don't know it was a lot of stuff around it and now I was not really much smarter what I did as next was I was searching for isolates so then I maybe can create my own one right so flutter isolate and then I found a nice video from Andrew and I watched it completely and then I worked better just a half of it because event loop is pretty clear and after that I was a bit smarter and the good thing is we have done the isolate part and a good thing is he explains also in his video that we have these compute function okay and I want to try this compute function now so I don't know if this will work right out of the box it is a bit trial and error so as I said this is a learning session so if there are any problems or if I make mistakes please apologize me and if you have better ideas that we can do just give me a hint ok so how could we do that what I want to try is creating just a container maybe with a scaffold and what I have is in my mind is I want to create a small application that has just the timer in behind in the background and as soon as we press a button we let the timer run then we close er off the application and I want to see if the timer is still running in the background just to see how we can forever with this ok so we have a column in this column we will have two things one is an expanded which has a child and in this child I want to have a button and here we will have unimpressed so this will while the chat is going active today great yeah where are we can explain me and what about Apple for sure because Allah mm okay perfect cool help each other that's perfect thank you so we have these parts children of course and here we go expounded and closing that off and here we have our text that will be our timer that I create in a second the second button should be something that we they expanded is here so the second thing is unbuttoned no wait this is not the button that we want to hear fear here we want to have just a text with the timer and this gets surrounded by a center widget and here we want to have the raised button okay so if we would work in a usual application I would just create a timer for this that I would trigger and after a while I would stop that to just see what is the result but this time it will not be possible right because we want to have these isolates and now let's go back once more just to take a look into the isolate class all dart code runs in an isolate and code can access classes and values only from the same isolate that means we can't access inside of an isolate any other isolates surrounding it different isolates can communicate by sending values through ports see receive port and send port this reference to an isolate usually different from the current isolate it represents and can be used to control the other isolate is a reference to an isolate usually different from the current isolate make sense it represents and can be used to control the other isolate okay makes also sense so how would that look like I think I open up pencil for this just to make it understandable what I'm doing here so we create a document and what we have is for example to complete rectangles right so here's one and the other one will be here for example just kidding it supports Apple perfect congratulations all right so that would mean we have here an isolate and we would have here an isolate and both of them don't know anything from the other the only thing is we can have in one eye so that like a reference to the other one and this gives us the option to understand what happens there so these two will send each other messages and I think these messages will run through these ports so receive port and send board the spawning Iser receives an isolated object representing the new isolate when the spawn operation succeeded I make that a bit bigger I think it's easier to read for you guys but of course the link is also easy to find so just search for it all right pause capability has no effect the isolate object provided by spawn measure all right okay so how can we use that actually and what has the compute function with us so flutter compute there needs to be also an API right I hope it's a property so we have a far future that something returns a function of that compute call back it spawns an isolate run call back on the isolate passing it message and avalanche eventually return the value returned by callback so that means we get even an answer from this computer that is amazing that means we can send something inside like a message and wait till the callback comes back this is us before that takes longer than a few milliseconds in our case maybe even minutes right because we want to have a timer consider for Tallis it will only take a few minutes a few seconds take the scheduled tasks ok so it needs to be a larger action that we want to do here so Q is the type of message that kicks off the computation that will be this one and R is the type of that is returned okay so these are the types of the computation well what happened so thank you all right so this is that okay okay so what we can do is we can just try this compute for now let's see how this would look like do we have in this path Jason maybe there is a good example so parse photos seems to be AB function I am assuming and response buddy there is a fetch of that so how does it look like when we search for pars photo yeah this is just an execution here I can't see message section in your okay cool so how would that look like here we have our home screen and in our home screen we have now that unpressed and this unpressed would now execute the computation right so can we use that just like that auto import yep that's fine and now we can call that now we have to create it and the first thing that we want to do is here comes the method inside that we want to execute right and the second parameter is the response body what is response body is just any answer from that used to computer run in a separate isolate and response bodies mm-hmm just a second I want to understand what happens here and also make it a bit bigger so this response body seems to be the true parameter that we want to give into the function right so parse photos is actually if we search for that part photos where do we have that so this is where we call it here so as you can see parse photos will return as a list of photo but it takes a string of response body and this will how it works so the first thing is the function itself that we will have and the last thing is just the enter the parameters so what does it says to position our expected but found one okay so it needs the second one interesting okay so we have to compute callback the message all right so what we will try is I would just give inside null let's see what is does cannot be assigned to future or dynamic okay so this one needs to be a weight most likely hmm let's create a real function for this so and let's say this would return a string okay just for now and here in the compute part we will have the test function executed and we don't execute the function right it's just the container of the function and it cannot be assigned to the parameter future or dynamic of course because it needs to be a future right it needs to be async and this will be a future of string cool but still it is red why can't be assigned to pair future or dynamic function okay I'm still not completely happy with this at the moment so how does it work use the computer and rerun parse in a separate isolate hamish how can I help you okay so we have to parse photos and as I said I thought the parse foetus is just the function but why is the response body is so important for that where's the compute function map JSON decode compute here and we find that parse photo somewhere parse photos here we go so it is just this method what do I don't see here is there any any mistake at the moment so do I have to pass something in yes okay so now it works so if the message the parameter is correct it seems to work yes so what Hamish is referring to I added to flutter explained our life into the loyalty shop some additional features down here so you will have tau points on the left side that you get by just staying in the stream and on the right side there are some things that I offer you as a gift if you have two enough points so for example for 50 points you get like an early access to our newest blog entry it could be maybe interesting for you cool but back to our topic so we call this method now in the background right so compute is running that in the background the question is now what what would happen if we closed the app down in the meantime so if we are saying we have this test function and what like to do is creating a timer for that I think that's reasonable so we would have a timer and I think it was a timer or not yes start arcing equals a new timer so how would that work I think the time I has some cool API so let's look timer has a callback and a duration perfect so the duration we just have here and in this duration we have to set now the seconds minutes and so on so I will give it a good minute just because I think my app to close it down and everywhere let's give it a minute and the callback is just nothing at the moment work and I yeah wait I have the timer the timer gives me a call back can I get something like I have to check how I get something back from the timer Bart timer free second start timeout at a timer in that case is not even in the stateless it's even far further waves in its own isolated at the moment so as you can see it is just a stateless widget with which ten cults is compute so it creates its own dart instance and then it puts Ted into the function down here so it is completely Dart and yes it would work in the stateless hmm so we have that timer the question is if everything is working like I expected that I so that that is something that I don't know we have to tested but more interesting is now what I would like is something like I don't know maybe we can just print that stuff here so give me every second the timing time a touch no not tick that makes no sense do we get a value from that mmm timer so the timer has a callback which is unfortunately a void Kovach so we don't get any information the code is after the given duration okay so this is the last information so here we know exactly when we are done so what we can do is just printing finished hmm and how can we test that now because what I what absolutely will happen is now when we run this of course it will finish in the meantime but the question is now will it really finish after one minute or not but this is also something that we have to test I don't know so we have that timer we do that let's see if this print will happen even because the isolate is completely separated it could be that it's also posting something in its own space somewhere so what I will also do is I will create here future thought wait no future dot wrote return delayed here we go duration also with the minutes one just to make sure that I get a 100% at have call back should I change it to seconds okay let's seconds and let's have like 15 seconds I hope this is enough to switch back to the application maybe you can send it message to the other isolation thank you mama for your help the method to the other isolation I could try that yes I'm just not sure how is it just a return statement it could be right but the problem is if I just return it I need like something inside of here right and the problem is still if this is a call back I will not get anything back even I but I can do something like this can I not make future a wait or wait for it or something with that I get a call back but here like you mean time up periodic but time a periodic is also just giving me a call back right if I return a call back I will not get anything is it right so if I use the periodic I still have here just call back that happens every second or so I think we take a look into it we have a call back of the timer sound current unequals okay yeah but it will just have a call back itself so if we return that we will not get that back into our own instance because we needed actually here right that we can then print the result for example so that would be the idea behind so this periodic will not necessarily help us I think [Music] I would like to have a future that we wait for well I wait like I notice will not work future dot delayed and what I can do of course what I'm doing here so and the duration is seconds 15 and we just say a wait because it's a synchronous right so we will wait anyway future string fetch user order future delay duration 2 seconds large lucky yes something like that exactly we have to call back then in that case large latte the problem with that is that these call back if we return something here inside we will not get the answer so what I will do is I will just wait now here this is enough because now we wait 15 seconds right just cool and then we print or better we return a value like hello world and what we also can do is the timer instead of this we can just say we returned the timer dot can we get there a two-string or something maybe this will work where we get the timing I know that we don't get the timing okay let's just start by returning anything just to see what happens actually with our app maybe the whole thing is not working even and here in that compute we say print and surrounding the whole thing okay if everything works well we have to await for this so I think no yes so we will wait for that the only problem that I see is that app getting recreated that's interesting so because the app is getting rebuilt most likely this part will be getting lost but we will have to look I don't know ok so now let's run something well that was a lot of stuff restart changed us change the device I will take the emulator so this could take a while I'm sorry for that already in the meantime we can talk a little bit so have you ever ever done that with the backgrounds background stuff so far or is it the first time that you hear about it revenue create sounds also very interesting I don't know the isolate should be running in the background so what this is meaning I'm not absolutely sure yet I think this is also something we have to discover right now Stefan so isolate on background or flutter isolate app sleeping yes banana case doesn't actually have to do with the background code over running code in the background is something discouraged from the mobile operation systems oh geez okay oh that's an answer at least that's something but this was in 2017 so it's quite old right so mariya be there's already something new 2017 2019 sounds better a work manager you can use the Android as a text string so you want to have you I think it could be easier to run the stuff on the server yeah that's true but how would you do something like a step counter for example if your app is in the background you want to execute code right so you have to for example have it access to these sensors that tracks your movement or not so if you would run it on a server you would not get these in force or you have to send it all the time to the server I mean why my name shows red background that's just for you image so whenever yeah someone Rhys writes your name then automatically you get the info see this bro and it's so true backward how are you yes stream yeah yeah it's a bit it's always a bit tricky if you learn something on the stream I think because that yeah it's it's bad for me because I have to say I don't know anything but it's even hard for you guys I can imagine because yeah you don't get directly a result right I can't explain it like okay this is working like this and let's go and I think my emulator is has some issues at the moment I don't know why I had that the last time also already in the stream so maybe this is the time where I have to fix it sorry for that so what I will do now is we're getting a lot of stuff I just want to kill my brittle device once completely so I wipe and make after that a cold reboot and I hope this should help yeah oh so it seems we have a complete different problem that's that's at least good information so code book now is already running okay and we're ah here we go that looks better okay so we close it again and my computer goes to the rocket yep yep yep it's a bit too much for him why is that now a separate screen sorry for that I just want to get rid of this desktop here way better mmm to publish an app - no you don't need one you can do it with something like CI CD we're from Cote metric for example they provide you all the necessary steps so if you have Android only a flutter app you can run it through ti-ti CD from code magic and after that it rebuilds your app also with iOS devices they my problem with that is that you contest it actually so you have to hope that everything is working or more much I think you will come in the same problems that I always have so could you sent me that example via Twitter maybe that would be fantastic so my twitter handle would be the flutter HP don't know yeah I will share the link as soon as I get it from momot or mahmud because the problem is I can't or somehow I don't know how to get people to send some links in to Chet I don't know why it's so bad alright so we have the timer if I click it we see we have a spawn running here in the call stack if you take a look so that's pretty cool that works already so if we wait now 15 seconds I thinks the icd from code magic is 500 build minutes for free and the in the month so it shouldn't be too too expensive oh cool and we get both we get even the string that has happened here inside the printing so it takes the same terminal which I really like anti-anything is working obviously this is now just inside of the app so what I will do now is i trick triggered timer once more and I'm pressing now to get off part and now let's take a look in the debug console after this is running through if I get the call back or not yes that's true image in the long run make is better for this stuff great so even if the application is not online we get the information that's pretty awesome I think now the question is this is now happening when the application is still there right so we have that in the background but if I'm pressing those timer and I kill the app completely okay the call stack is getting dead so if we close the app completely we lose the access of that that is pretty good to know I think so at least we have some kind of control of the device right so we have the possibility to send something in the background and leave the application running I think the biggest problem will be if you close the application completely down that makes a lot of sense I think and that's already good enough I think for a use case like that timer right so because we can do here in anything inside so for example if we use a periodic let's say periodic timer and instead of doing no or better we use that wait a second my Twitter account doesn't exist what don't tell me that I'm long time in Twitter so many requests miss your hobby perfect this is the flutter service so I post that here right now and the other one is this one and this is something else I will take a look later to that cool but what do we have here actually so we have these parts this I can close now so what do we have so what is the our question how do I run in the background even if with the screen off short answer is no it's not possible move that sounds terrifying I have to observe the different behavior for the display going to sleep the following code will help you understand the different states of a flutter on android tested okay so framework revision engine but this is also from 2017 or it's pretty old yeah it's already 2017 okay on deactive is also existent interesting app lifecycle stage okay that looks nearly like our example or I mean they want to have a timer here and they counted it up inside of the application when launching the app developer count is I need to make it a bit bigger sorry for that so hmm so is incremented every three seconds okay a text below the counter will show changes or care so here the changes are there so we pause resume and we pause resume so it is nearly the same like we did or timer counter they use one returned to flutter timer counter value free flutter I presume wave 7 okay so when switching to another app or when pressing the power button to turn off the screen the timer continues to run but when pressing the back button while the flutter app has to focus the activity gets destroyed and with it the dart isolate okay flutter up life cycle and Android life cycle for fluttered widget layer only to pause and resumed state are exposed okay that makes sense oh yeah that sounds right okay so what I read so far or at least from our Jupiter seems that we have some issues here at least with some as I said with a full Clause of the application seem to destroy the flutter application completely but we have still the possibility with pause and resume so that we get at least some information back which is pretty good at least right so we would be able to make that actually or so we have that life cycle made sure that we don't need we have this part but still that would mean that we don't have any chance to for example have a step tracker a correct one that maybe runs offline or separate so we would need to write a plug-in for that and what that would mean means that we create like native code create a flutter yep but right native code that is getting injected huh okay so but for this we would I can just copy that same thing here more or less flutter services method channel a name channel for communicating with platform plugins yes I think that sounds about right did I miss read that somewhere it was that in part of it so switch it between a process that's good flutter services method channel a name channel for communicating with platform plugins using a synchronous method calls exactly but where did you found that said some part of your so here's a even a package recommended here we have how do you run tasks in background and flutter there are a couple of things you could try now this is from Andrew perfect one is to have the Kotlin function do its work in background fret using the methods Android offers for the editing task for example you could use the method channel to handle community between JVM and dart after coddling code sent a message when it's done thank you Mohammed mmm-hmm another possibility is to use the dart isolate to multi thread on the dart side this is what I tried or not you'd create an isolate make to call to Kotlin ah alright and then we would create the Kotlin call and your other core dart code could asynchronously wait for its to be finished in the UI thread while still running the event queue the flutter team has an example of that how that might work are this is the example that you talked from that is fantastic so I make it a bit bigger again we can read together okay now let's see what we have here but I would mean we would have more like that or this is it not just the isolate itself here we have a calculator okay on progress listener some stuff number of items not far interval and they work now with this receive port and request port right that we had in the beginning [Music] run calculation load string data Jason okay we call an animation controller okay so it seems like this example could be usable from us or not so what would happen if I just say I take the whole example here and want to run that from my application so this is a service layer the isolate example widget did I is that somewhere here inside layers no it's even part of the flutter gallery oh hey there welcome to the family yes should everyone absolutely right but what I could imagine is that if we send it or pass it to Copeland for example and we have it running in the native code and we would say something like please keep it in your life cycle then we could kill it maybe but even then that would make no sense or that would be also very bad for the for the layers not sure with that [Music] so but what was his isolate example widget didn't know I see that so there's the library rendering service minded orange layers I think it was under services isolate Dart this was the example and here's the example which it ok so we have a state here with all the stuff inside like the animation calculation right idle and start is the status in the label and what do we have are you mean that we can I should make that bigger that's not what I want dispose build we create our animation here okay opacity erase button handled button press progress update state update stage calling the calculation manager stop and start absolutely stuff and I think that's what isolates are for actually so if you have like an API base you want to called it I think this will work the same way the only thing that we could maybe do is like reinitialize the state so when you close the app completely and you restart it that it starts there where you lost it or something like that okay hmm so after that success from this morning I'm a bit disappointed that is not that perfect would be great that there is a better solution but still if we have that part so anyway what I could do is I just take the whole code or not when I'm reading all of that and that should work actually at least something should work so that was my intention or my idea behind so we have the update state we get results we make a set state the calculation manager whereas this is coming from it is a calculation manager okay not really helpful in the comment main app widget the apps shows a simple UI that allows control to back your as well as the animation to illustrate and when you write an app without a new AIA I think that shouldn't be a problem or not if you work anyway without a new I it's just dart right so we've dead you anyway you have all right so let's it is an isolate for itself right so this yes that your eye does not block while this computation is performed so this is perfect for some parts so the calculation manager where it comes from this is the isolate itself also the whole part on the top is the eye a computation manager calculator manager okay I see calculate state calculate message calculating yeah here's the manager so we get like a progress listener and result listener how to create apps without UI it's like the dark Cohen's that we created right you just say pop create yes and let the isolate set data to the primary app yes exactly but this is nearly the same like we did or the only difference is that it keeps track of the progress because if we are taking a look back here with that compute we do nothing else than creating our own isolate right that's a good thing so that's me meaning if I'm clicking that the only thing is that I have to track now is the time that is elapsed in the meantime I believe at least so what I could do is make this a stateful widget and let's have here a string with the timer or with the timing which would be for example empty string and this timing can be used at this point here oops timing I will give it some style just make it a bit bigger now I can't read myself anymore here we go so text style I have a size of 36 for example now everything is a bit bigger did I is an empty string obviously so what we'll have is zero zero zero zero supply this hey Ashish nice to see you your reference in in chat is different than others well I'm sorry I don't promote enough my live streams it's because I'm always nervous about them so ad timing timing is the text it should be visible why is it not so go hot reload hot because it stayed for widget here we go okay so now when I'm pressing my button I would like that this timer is updated all the time whenever something changed instead possible no because if the help is getting down hmm good question I don't know so what would happen if I give timing inside here so and when we run this I will use the periodic and what I will do is I will just update to timing all the time I'll most likely I don't have then yeah the app has to run and additionally I think if I want to set stayed all the time how should it work if this stuff is not there hmm so I have to rebuild everything at the end okay see this bro I'm not sure what should I see okay so we have still issues that we can't solve or not that easily at least sadly knows the links are still not postable unfortunately so if you want to send me links please coming to Twitter I think that's possible Thank You Robbie I will check and here we go flatter tire below grade so I take that and bring it down on schedule periodic or dynamically generated time events okay so time re builder periodic okay so I can just reuse that but the problem with that is I can't let it run in an isolate right and then the solution passes the question we try to solve links that possible I send it to true sorry I have to quickly go through the chat sorry what do you mean Stephane I'm not absolutely sure so this would be great if we just want to have a time I write that that's fantastic so if we want up there at timer yes and give the end time to the back ah yeah that would be an option that we just say something so after you are finished send it to the server that could be a possibility so in that computation we would do something like okay we wait it now our computation is done send it to a back end but I think what's oh and we is it possible to we could even do something like send it to shared preferences or so we could use shared preferences in that place where so maybe we have a solution for Android even today for Android today so when we come in here to pop the Deaf and I just use now to share preferences here we go now at firebase the problem is I don't want to start with firebase o else we have one hour maybe I can do that but let's think about it let's first keep with that and try to keep it as easy as possible and then let's see if this is working and then coming to the next heart apart okay you know what I can really do that so let's check it out I'm going to this part we can do it all right so let's make a pole for that at a pole tunnel okay so what I will do is I will have the question which database will we use or should we use and then I will give the option firebase come on firebase add and we will have SQL flight I think SQLite then we would have of course shared preferences but I say just shared so that that you don't have to write so much the native database would be the SQL flight air-cool no sorry the shared preferences or not hi if ok why not if I'm if I'm getting crazy already that let's go that is absolutely correct Ashish and I'm absolutely with you but I will do whatever the community wants so now let's try if we get a poll done all of them are working absolutely fine and actually for this minor problem I would really say ok let's let's just use the good old share preferences in usual cases but let's have some fun today the worst case we take the biggest database that's not worried straight but it's great to see you around even if I'm talking all the time English cool so what we can do now I don't know if you ever have participated in such a poll is you can now write exclamation mark and then you go with writing the name of the vote so whatever you wanted to vote so something like how can I say that use though and then you say something like for example hi let's be crazy that would be like your vote today yeah I see yours we have one for firebase at the moment now 5050 with hive so give it five seconds in the mean time because I is it SQL I don't know I think I did it with SQL flights or HD fun and instead of about shared preference it would be shared so you get the I don't know if you can see the but up there I maybe write it once more and then just keep it with that Soyuz exclamation mark vote and then you have 2 5 4 different options and in the meantime I will quickly go to the bathroom so give me a we will see us in a second you oh sorry so okay so where do we stand I take a look and what has changed that's great to have some fun with you guys it's perfect hey Daniel and where are the poles here we go so currently we have a head-to-head with shared preferences hive and firebase and Daniel you have to do an exclamation mark vote and then shared then it will work same for Stefan if you want to ask excuse light you have to make exclamation mark bowed and then something something like this okay let's give that another two seconds in the meantime I can try something else and just have a look here if we find a package that has something to do with the with the work how does it cold ah here we go finally some votes revenue you know what I wanted to do is isolate so here we go here we have also a utility class for the related isolates that's cool interesting oh I hope you can read everything fine with your phone ok the weird thing is it seems high fat won a poll which sounds crazy because I don't have any clue about it so I will complete that now so let's see if we get the answer if everything works with my tooling that means you can no longer do that and hive was the most voltage ok so let's take a look into hive why not what is hive database and how does it work and flutter fast enjoyable secure no SQL database perfect okay so we have a package for that and it is a lightweight and blazing-fast key value database written in pure Dart that's perfect so close blah blah blah getting started quick start here we go head to your project - hi flutter all right this is something I can do very easily there's a high for built generator that's cool that means what we can start with is going to the pub spec to ml and as always we add these woops sorry for that I think I can make dead one I can close that one off in the meantime because it will take a longer time till we have that - the part and if we have that we will have the dev dependencies also which is important sorry mama unfortunately the poll was winning four for Haifa I can't go against the Democrats see that would be really bad for me yeah that's no problem we will save it as a string then later that's no problem so what do we have to do initialize hive in it flutter okay with a valid directory in your app files you can also provide a subdirectory okay so we will take that and hive in it for not flutter okay so let's go for main guitar edge and we have to go on the top here because I assume we have to do it right away so whenever the application is starting up we get everything that we know I need and in it flutters red isn't defined for the type - tur face okay it seems Oh [Music] has something changed you maybe okay let's just try in it how does it work we just give two puffs and there seems no in it anymore Oh unfortunately you know you you had to vote like something like this so that would be the right vote sorry the next time you will know and then we can have shared preferences because it was really a head-to-head race sorry everyone so cool this is dead okay so we have that unit and now where's in it flutter and no not necessary in the browser okay I've by giving it a home directory so we have to specify the path and for the path we will of course use the puff package from flutter do we have it here no so have any so I'm pretty lazy I just take here any of course you would take a sophisticated version that you don't make any mistakes in your productive app right and here inside we can now use puff and by import a puff and we call that power dart cool and what I will do is inside of my lip folder I will create another folder that I called rusty be fair enough restart the poll you really want to take the time so I will do that as path that we have a directly work here and what I can do is I think there is not only absolute there will be also relative relative path and now I can do something like dot slash DP or dot slash lip BTB I think that should work hopefully come out coffee oh that's true I need coffee I somehow I really need a coffee machine besides but it needs to be very silent that you don't hear it all the time okay so now we have our hive I give that a variable called hive here we go and I can do stuff with that I assume so like stuff at no unfortunately not don't tell me you have crazy ideas guys all of your data is stored in boxes okay if you say so Dan let's open up a box go here we open a box of our box a wage hive should we use at this is this even returning something oh no it doesn't so that's of course crap because if we know that we don't want to give here something inside we just have to wait till this hive is initialized and because it is anyway boy it we can go with it sorry but I will be back in a half an hour all right I send you github example for hive with flutter you did choose you are really fast thank you ah I think I'm on that page already or that is not this one but this one here exactly you can use this way final directory await paths provider get application directory hive in a directory powerful all right let's do this way so what is the name final directory Thank You Chuck Jack mackee it's correct I hope my French is still a while ago but should work like that yet and I think path providers also a package right so let's go to the pub speak Tamil again and path provider any sorry for that and here we go path provider no pup get failed was failing - Raider and flutter test any from SDK version oh geez you're okay so we have to be more specific with that contrast to whatever we want so any is not a good solution for that let's go to popped up dev and search for what did I say path and path provider installing getting the path here we go and the other one was path provider and I think there was a flutter favorite if I see that correctly here we go installing and back to here so now maybe everything is better hopefully background tasks depend on flutter test any SDK so he complains about testing here [Music] because every version of flutter test SDK do is something wrong it's confusing right now wait do I have to put that on top it has something to do with the path obviously all right so what can we take here that it works I have one six four all right so one six four let's try this ah no worries it's cool then we learn all that something no worries I mean I learned it's also my first time so and because every version of flutter depends on quiver to one free because hive dinner Aida dart X which depends on quiver which are okay now I destroyed something I think so where's the problem is it because of the hive generator right now that would be really bad Hey Elias nice to see you this is a weird problem of dependencies and I really hate that so hives in rater depends on which depends on quiver okay so then take your quiver I I don't mind so important weather wait instead apart let's go to the hive once more and check the installation and if there is nothing so do we have the hive yes exactly but ah do you think I just have to increase the SDK version here hmm no yadu dependencies have like a dependency and it needs for example up here it needs quiver I think so I hope that if I'm searching for hive once more here and I find hi flutter I find more in the install nodes no external please go now that's unfortunate now so we have to build runner we have this we have to hive did it work before when I didn't add the SDK where the path stuff or was it already broken now we still have two problems here - reiter depends on our X star a dart X which depends on quiver every version of flutter turn depends on quiver - one free RK you know what I will remove the flutter test for today maybe this helps aha here we are so we remove all the tests and everything's green by the way if you ever work like that in your production system you hopefully get some problems exactly I just wanted to add them because it's anyway run any and okay but now we do the puff stuff again that we wanted to do in the first place so we will go here and that should bring them back and I remove the tests again and now the pop top def should be fine ah beautiful so now I have to scroll a lot up to find your solution once more I think I here it is and just copy that down and let's see so we have to find a directory we wait for the path provider and the PUF provider comes also from an import here we go and I think I say path provider good and now we get the application documents directory so that should be the main directory in that we are and then we say - it yes in indeed but what we will do is we take the directory I think I can do that here and was it not the same directory path okay all right so we keep there to remove this okay cool all right back to hive we can remove the path in that area but we still get a box right we open a box and we call it in test box yeah I think test box is cool I call it flutter explained box just because I can so we have this box now running and I assume the box is something where we keep all our information inside right so everything from it and what I can see is we open the box after that we put something in and then we print the whole thing oh here's even some video from reso coder that's cool so if you want to find another cool flat channel reso coda is really good so highly recommend cool we have this we have the box and now we can say box dot put or something right so then we have a key hello and the value is world for example and now we could get this information so the question is now is yeah hi should be in singleton right so that would mean if I'm correct this init initialize the whole hive part and then is it a singleton is an instance we will see so we initialize that once I'm not sure about it but what we can do is now inside that part where we want to have it we can hear and they compute we could access the hive dead part here right or not put for example I know we need a book at the books of course wait here so this is the way okay and this one is in the timer and I think we can't wait at the timer can we what is our goal actually why do we need a data face we wanted to save the database variable inside of hive why we compute everything and after we restart the application we will automatically write it in the hive database so we read all the information from hive is that correct did I remember it correctly is that the reason why we use it to put the endtime okay so this would be it did end time that would mean we will have here date time dot now dot not to string just in milliseconds since epoch I think that should be saved here and now we will have here inside our milliseconds okay this what's not too complicated it sounds familiar but now we have real DB right that's that's pretty awesome and restart i read the hive box exactly so whenever we close up the application completely or better whenever we initialize this state here again so is it here yes exactly so whenever we initialize this state here in its state we would then ask this box for can we get yeah perfect when you get a new time on a pending the app you calculate the delta time okay but for now I just want to very look if the hive stuff is working no worries it's all good when you get a new time on appending the app by opening the app you calculate the delta time yes that could work but for now I just want to get the information back so what I have here is just the time that timer end time and I think what we will need is to get the whole thing here and this will be an integer so this is our milliseconds and here what we can do is print the Milly's no we want to have the date time to a string okay so what I will do is the following we can construct a string or I can do that easier but um the first thing that we have to do is daytime dot from milliseconds milliseconds since epoch milliseconds inside and now what we have to do too is state I think we have date format if I have that correct in mind date-time format know how was it so this gives us back a real date time right so this is a date time T which contains all of that I can make that a variable I think it's easier cool and this D I can format by format ah yeah start at the interlibrary yeah that that's that that was the trick okay so we need the info library for that what I want to use it for up do we need that no I think that's too much so what I want to do is the timing is a string where we say something like T dot hours cool so and I think I have to interpret it correctly our then we have the d dot minutes and the last thing would be whoops d dot seconds okay and after that we have set state done do we have to call said state anymore I'm not sure I think I can clean that one up right on the other hand so and the worst thing is if we don't get anything from this box get right what happens if this is null if this is the case the milliseconds will be here hmm empties or null mmm so if milliseconds equal now then we don't do all of that stuff we just say map else timing equals the default value hey sensor I hope I don't do crazy stuff right now and you expect something else from with me so where do we stand actually so at we have 18 viewers that is absolutely fantastic and a new record so thank you very much for everyone who's joining today that's crazy and what I'm doing at the moment I'm working with hive the first time in my life oh god they somehow pushed into it and what we want to try is to catch the data and what I'm seeing right now is that we await here inside of an init state and what will that happen is that we throw an error so we have to do something like correct Robbie perfect should be unequal now that's even worse thank you thank you thank you okay but still we take all of that and we need to create a function like in it timing suck and this one is a method down here and here we do all that stuff and because a method can be a synchronous this is happening but now this will fail so we don't need that actually we will have that as a default value and what we also have to do is now set state close enough voila yeah Krishna how can I help you watch which is the query how how can I make you happy so now we have this part this is running and here is very important that we get this zero zero back or else we will get a null pointer on that area whoo all right so I hope you enjoy the whole thing that I'm doing here even though that yeah I'm pretty sure that we will not manage to get a background process that happens like we expected or that we get like even a step tracker or something like that at least not with the idea that we had in the first place that we will create everything from ground up but I think we can have with that already a lot of fun with these compute funks things what is the next step next steps hey motor India I think I saw you a long time ago have you not been one one of my first streams where I made like dart the dart Cohen's or no the dart what was the other thing not true anymore it is easier than share Preferences really so it is a real database this is what I really like so I think there should be some some file now somewhere from that hive and when I started up but I'm not sure if it's easier let's see okay so this is all working most likely this will look a bit different than this zero syntax that we have here so this will be a problem most likely because if the our for example is 1 p.m. then we don't have a two digit number and it looks a bit weird same for the minutes and the seconds but that's alright for today we can live with that does knowledge check we can do something yeah we just don't do it yeah that's that's fine so let's start the application and let's see if something works I will take the iOS simulator just to try if the computation is working they're exactly the same and we can use the hive they're also the same way and then let's see how it turns out twenty Viewer jeez make me nervous the complete idea is to calculate the time the app was closed okay so this is our idea thank you finally we get the idea so at the moment we only save the time so we have to get like my set state is not doing anything such state don't to have to do anything it marks our current widget as rebuild so it marks it as dirty and it really builds it so you don't have to put anything inside it's just if you take a look into it then you find down here the mark needs rebuild and it creates the build method once more in the next frame hey Shiva nice to see you around the complete idea is to calculate time yeah so let's I think I focus a bit on that part I thought I write that somewhere as it should you because I will forget that 100% again flat error if you are running an application yet that that happens from time to time so you just copy this part I think and you just put that in front of the main in front of whatever you do here and you release the debugger is it still broken yeah cool so kill it completely and restart I make it a bit smaller more may make the different make it a bit bigger Thank You Ravi really looking forward to hear that if you have any feedback if I can improve somehow if the voice is too bad or you can't see anything correctly just give me a hint I heard yesterday the first time that some people can't read my font or and so on so I increase the one sighs this time all the time so hopefully this is better and also this year I have now but I don't use it which is very bad so remember me on dead server press now timer we have that background task running and it should save everything into the hive database and whenever we rebuild this this widget it should we go okay crashed at the moment you need to initialize high4 provide a path to store the box okay so do I have to do this - I'm at the beginning this part here so most likely we would create something for that like creating a single turn out of that but for now I just put that here and also here because else both will break close it off again I think that should do the trick now let's see sorry I have to stretch a bit during our wait time so running Xcode or do I miss light right now something on the end will be timer time at close new time a complete timer okay so that will be inside of this test function right so what we need is still the information okay wait one step after another I wanted to write his introduced and I think I would copy exactly what I would have on rights right now so I can remember that easier exactly almost bedtime say for me no no okay this is done so we have to time our clothes complete timer that's good and now what's next step so I'm using this for in it yes so in it flatter the problem was that when I have this in it I can't make in it flatter it doesn't exist in my my case did I miss anything do you think in the pub spec Toyama I need to add something I have that high flutter here so I thought it should work here let's see okay so I have to import it as yeah yeah yeah thank you thank you thank you perfect so now I've and then flutter no what is the name flutter hive I flutter okay I take all that stuff here and let's try that so and with this we have now in the main function the possibility to receive that in a directory and we go back to our main door tart where we say instead of in it we say in it flatter I look at that so beautiful okay that makes so much more sense and we take the directory and we bring it also up to that part this directory can begun and this hive can begun you open the pod yeah but then I have to pass the Box everywhere right I hope that this hive here is a single turn and with that we will have access automatically to the open box at least I hope that else we have to send the Box everywhere to all the widgets down below I think it would be great with the it would be great if the hive part up here would be a singleton and else we could do that with a provider package I think where we get everything inside uh okay we have this now that soft as a soft where's my here we go okay we restart everything just in case rebuild and let's wait a second I sent a flutter hem still on the hive we are still on high making multiple yep so thank you Bobby for the link we'll add that here so I'm using have to make simple croute in hive talk about open box we can declare like la box you'll be a blip oh that's pretty cool so if you want to have multiple boxes you can have that as a box list and then save that as a oops you need to initialize high4 provide a path to restore books actually I hope that I'm doing this but it seems I'm doing not what do I have to maybe do it here a second time because we are in an only isolate could that be a trick life on hive absolutely no yep exactly I got links right now from from him via Twitter I I attend also here I'm sorry that we can't pull him here inside okay so where are we we want to open up this directory in the init flutter so can we use the whole thing inside of our isolate once more so maybe this helps know it breaks at that time in init timing resonant timing notices [Music] that's unfortunate so we have that open box we get the box but unfortunately it doesn't work like I expected to you so that's also right so we have two directory we get the path so it should do it now what do you think actually for the flutter days I'm all the time thinking if I should make something on flutter days or if I just maybe a timer cannot wait where's the wait the timer down here this will work and this one isn't this is just a callback of that timer so whenever 15 seconds are derated then this thing will happen or getting executed at the moment so it is just a background task where's my okay so let's restart a thing and hope the best-built arrows existing okay cool no worries it's all good I mean windows in life I hope that we get like a webcam or something in the future but at the moment I still have to to save some money to get all that stuff together because to make the Windows PC running and up and running with webcam and everything we really need some some equipment so you would be interested in a fluttered a stream or something like that I was absolutely not sure so I I think there are so many good speakers around and also mocked up held to speak so she's invited for a speech and she's very nervous so I hope you will help her because she will have a talk with the GDG and it's still broken in Tara you need to initialize hive or provide a path to store the box so I was send actually the link then when she has her talk she get it confirmed and everything it's really amazing I tell her not to be nervous but I can't promise anything else she already tries and and trains and everything it's really good what she do the flutter day will be at a 25th of July I think and um so we have still 19 minutes left and or 18 minutes left and I would like to solve a problem here and somehow nothing works with that hive which is unfortunate not true lies or wait wait wait wait wait let me Google that not that I say sorry wrong it's this week or this month so it should be assumed here 25 of June so sorry for everything false information false claims no no it's the 25th of June and the best thing is as soon as the June is done as soon as the flutter days in June are done then the best thing happens because then heck 20 starts and I still make a lot of advertising for track 20 because this year we have great themes I think there was a Twitter post what they have posted is that their theme this year is like saving the world and the second part is cyberpunk I think that's pretty cool so worse cyberpunk I think was the second theme I'm not sure anymore but it was pretty cool and to solve their some problems over the weekend will be amazing so I'm also looking forward but my idea and I don't know that is I'm as channel yes but still I'm feeling not kidding provide as much value to everyone that I can take the time of other speakers of the flutter community because there are really a lot of them and they they have great talks prepared but yeah let's see let's see what we will do so in it timing we have that we wanted to have this the hive is not working why is hive not working we await at hive open box accept directory path maybe not correct so what application directory dear path in it and we do that with the directory path that's is there anything that I absolutely don't see you that that I make may be a big mistake that should be easy to solve I feel a bit stupid right now make a breakpoint that sounds like a good and feasible idea so we will go to the hive dot it's this part right that's break so let's make a breakpoint and up here there's nothing so that's fine so let's run the whole application once more what is this now okay I can also do this print but the problem is it doesn't return anything it's a weird problem I have to be admitted so it's really weird okay so you need to initialize hive or provide a path to store the box ah sorry not so often that I work with where can i press debug on is it this one so this is the problem that I don't work so much with Visual Studio code so now how do I run that whole thing in debug Chrome attachment flutter launch attach to device nope [Music] is it up here no hmm so we were so successful this morning with the dark Cohen's but this is not with in it flutter you don't have to pass the path really oh thank you jack that that's if this is solving the problem that way that would explain so much yeah that looks it's a sub-directory but of course we don't have that because we are in a flutter mobile development so it needs to know itself where it saves its data okay so okay and here we have the second one yep all right Hamish I think the emojis are on the code at the end so these are two numbers representation so that's why most likely they will take two spaces and Shiva yeah I think that innit fluttering from Chuck soft issue but thank you let's see if this is works working so we run it in flutter have our is running here don't know but I think it's the same problem like and I think on Twitter if you write an emoji then you have a multitude of letters I think I do the initial call right so here we go now we are in the break point happy days go for it you know hive to provide a path you need to initialize high for provide a path ok you know what I go to back to the hive talks maybe there is something very obvious that I don't see we get the box no need in browser ok here store objects ok and it shouldn't be the trick I need a wait no I have a wait I wait here ok so you can see it's my second stream today and I'm pretty exhausted right now and my brain don't work anymore like before so sorry for that okay so we await here for the hive box up for the hive in it flutter do we have it a second time yes so we make also here in a wait or even better because I assume now it will work correctly we make that up here and we remove the second time because it's not necessary anymore yeah I don't want to leave you alone in the mean time not for the last 10 minutes but after that we'll get coffee thank you Peter that is exactly the problem honey is so fast with its links perfect so I had that too so if you want to know how to debug in flutter f5 then yeah okay cool wait I can also show it here so and open it up oops okay and here we have our application out and it's running we don't get an error which is amazing and now at least I hope we can save the data inside so if we take a look into the main and we do all that stuff with boxed are put and get everything after we have started clean that we crash the application obviously okay so it tries to open it up but we need to initialize it and now I think we don't come around to just take it one once more because in the isolate it don't have access to the in it apart so most likely that is the reason why it's crashing here so because the isolate is a creation for its own right we have these two separations and in singleton from one isolate will not talk with the singleton from the other mm-hmm I don't even have what a really bad prepared so so yep fn5 flow so let's press that once I think I will use that automatically right because it runs into the breakpoint so I thank your Android method not found man what does this mean on what reload okay so there is everything going we're an hour was born it was super fun it was really exciting even for me to have 16 people around it's like crazy so thank you very much for joining today oh and here's the breakpoint so we are in it and I let it released and we get the error again what is this now native function windows set needs now I think we have the problem interesting so we will not have access to share preferences and we will not have access to high F in that area I think because I think that isolate is completely outside of everything so we don't have any excellent access to the parts inside of the main application inside of that isolate down here so we have to use a different approach so I where is it the isolate here we have our compute isolate so that would mean that we can't access unfortunately anything that is from the device so high will not work share preferences will not work that means image is right firebase would be the solution I think firebase or a real external web server so that we have like an API that we can talk through hmm I think that is not the reason image because I think we could also use aqueducts for example so if you don't know aqueducts it's amazing tool for flutter or better for Dart because you can create your own multi-threaded art server-side and I highly recommend to take a look into it it is fantastic work with it multiple times love it I don't need to isolate that's correct I think then we can just pass it through but yeah I wanted to try something with violets so I did everything today I tried my best didn't worked out so hate ah nice to see around ah so you are perfectly to the end of this session today so what did we do today what did we try so we started our whole life stream by trying to compute and creating a how do you call it how to we wanted to create our first isolate so that means an owned art instance that takes our heavy loading then we created a test function for it which we wrote down here which should just save the timing duration how long the time is getting saved till that so the idea was to end your calculation timer plus time a closed new timer complete timer so that was the idea that we wanted to solve inside of that isolate and we wanted to save that in a database and that came up in the in the middle of the stream so we came up with the idea let's create a hive database or better we had four databases for example like firebase now high that we used and share preferences unfortunately we learned and this is very important an isolate has no access to the device information so you can't access to the to the device database like share preferences or so on for the calculation I need to add like time ok I will do that like time so but anyway we tried it and I think we came pretty far for all that stuff that we did here and I'm pretty happy that we made that together that journey we learned a lot I hope thank you happy for your links Thank You Stefan for working so closely with us Emmett and Shiva thank you so much for joining everyone from you that helped a lot also who saved me today was I think Jacques with his awaiting and also his information with the energy flutter that that helped a lot so thank you all everyone and see you the next time see ya guys Oh before I forget because I added also two things you are very welcome serious guys all of you but before I forget I have there something new that I can add in chat because I got always the questions hey when is the next stream I added followed the following if you enter exclamation mark next stream I hope that my stream laps will be clever enough to do what I told him ok it seems not to work here we go so you see I added these things they have a global cooldown I think so they will not just spam all the time but what you can see is we get to make streams we get also how long the stream is already active and so on and so forth the stream has been active for parameter nav Abe so exactly start spamming guys perfect you know how I love it no but I think that's something that we can use for the future and there were a lot of questions regarding this so I think that should help help everyone ok so but this is it now thank you so much see you the next time enjoy the rest of your day and the weekend and I'm really looking forward to see you soon see ya
Info
Channel: Flutter Explained
Views: 7,371
Rating: undefined out of 5
Keywords: flutter, flutter tutorial, flutter execute background, #flutter background, flutter background fetch, background services in flutter, flutter run code in background, flutter basics, flutter android, Flutter Explained, Flutter Live Learning, Flutter Live Code, Flutter Background Tasks, Learning how to work in background, Step Counter in Flutter, Preparing for a Talk, google flutter, FlutterExplained, learn flutter, flutter dart, live learning, flutter learning
Id: cV0pByqNV6A
Channel Id: undefined
Length: 119min 21sec (7161 seconds)
Published: Sat Jun 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.