workmanager in flutter | background services | Shedule Task | Froground Task in Flutter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends assalamu alaikum to all of you today i am going to discuss about the work manager so what is the work manager and how we can use it in the flutter so in a simple way i have just placed a definition here and you can see here work manager is intended for task that requires a guarantee that a system will run them even the app exists for example if you are going to run a specific command or you are going to operate a specific operation even your app is not running in the background still you can use the work manager and you can call all the functions using the work manager for more definitions and all other things you can consider this thing and you will see here uh it simply says that schedule task with work manager okay so work manager is recommended solution for persistent work so you can read all these things even your system is rebooted and still the background process will work whenever you're going to perform any background task you will use the work manager so you can see there are some persistent work is immediate long running and deferable so there are two types of main work manager types so there's a one time if you're going to perform activity for only one time you will use the work manager okay for one time function and if you are going to perform some operations on daily basis or a specific interval of time you will still use the work manager even your app doesn't exist okay so let's start and how we can work and how can we use the work manager so first of all you will see here i have just run the application and i just create a simple application with the main function with the my app with a stateful widget and yes then you will see here this is a material app and that thing okay so i just create here a new state less widget okay and here i will write here the home page so um this my app should be a stateless widget okay uh it should not be it doesn't convert into here a stateful item doesn't have any option but it's better the my app the first uh the running app the first app should be the first screen uh should be the first video should be the stateless widget okay so i have a stateless widget here okay and now i just write here the scaffold okay so just put here the scaffold and after after the theme i just put here the home and i'm going to write here the home page so you will see the home page as a white screen okay now in the scaffold i just put here the body and i just right here in the center i can create here one function okay sorry one uh button uh let's consider that this is the button at the elevated button okay so elevated button have the on press functionality and i'm going to write here the child of the text and just right here uh execute or uh schedule tasks so just right at the schedule task so you will see in the center i have a button here okay now how to use the work manager so let's start with it if you are new here then kindly subscribe our channel and press the bell icon to see more interesting videos so how to use the work manager first of all i have to go to the dart packages and just right here in the pub dot dev i'm going to write here the work manager so you will see the work manager just press the enter and you will see this work manager okay so how to install it so first of all go to the installation and just copy this line and paste it in the terminal just like so not in the terminal we have to write in the pub spect.jaml so just write in the here and paste it and run it okay after successfully importing all the work manager libraries uh now you have to go to that readme file and just read all the things that you can see okay for the android setup you will follow this guideline and for the i o setup you have to follow this guideline okay so these are the things that you have to follow and here uh just remember that its kotlin version should be the 1.5.0 plus and in the android manifest your android name is flutter embedding and the android will value too it's almost there in 2022 when you create the project both will be updated into according to this okay so uh after the uh for the ios you have to follow this this guideline so these are not so tough so after this how can how can we do how can we perform the operations so um you have to follow these guidelines also let's make it workable for you okay so uh what i will do i will go to the main. file and here because our background task will work on the main thread okay so on the main thread what i will do i have to create one function okay so uh how to get one function let's create a function here and the name of call back dispatcher okay that will that that is the function that will run in our background okay so in this function what i have to do first of all let's create here a one variable and let's write here the task name the first task name that i want to perform okay so i just write here first task okay so just write this one now here uh what i need to do i need to create i need to perform some operation this function and what i need to write here i need to write here that i need to call the work manager and then put here the execute task okay so execute task will accept two parameters the first one is the is the task name and then what is the input data that we need to follow so just right here and let's start something writing here okay i need to write here like you will see here this is this function uh needs like this function uh return something should return something so what i will return i will return whatever the things come into the future as a value and put a true where okay so you can read this one why i'm writing here get a future completed with the value okay so whatever the uh operation performed in the future it will rate her because we are working the execution task in the future so that is the function of the work manager so i'm just returning the future value okay so after this what i need to write here uh in the execute task i will write here a switch case okay so this is the switch case i'm going to write okay so here i need to write it the task name the task name that i'm going to perform whatever the task name that it has okay so first case so let's write here the first case okay so the first case if the first case is the first task like this one okay let me put here that t capital so just copy this and write here the first start if the first task the task name is the first task so what we need to perform okay so let's create you need you can write here the print statement or you can write you can create your own functions if you want to perform multiple things so let's write here uh send send data this is the function that i need to perform when i click on when the first this case will be met so i'm just going to print out something so i just write here the hi so ah now here i'm going to write here the send data function okay so this is the thing that i'm going to perform okay so um after this call based callback to special function have been completed what i need to do i need to go here okay and here first of all i need to initialize my work manager okay so just initialize it okay so this initialization need one function okay so let me tell you what values it will accept it will accept a callback dispatcher okay so it needs function so i will call that function so let me paste that function here okay so after this what it says it says uh it says the call back dispatcher like this say okay so no need to define because it's already a function so you can also write it is demo debug more true uh what is the meaning of is debug mode true if you put the demo debug mode through it will show us the notification whenever the operation will be performed okay and now after this uh one thing to remember that you have to write the widget for letterbinding dot insecure initialize this is must actually okay so we have to write it uh we we have a main function of the async so we have also right here the await function as an avatar now just go here and let's perform some whenever the uh this schedule task will be clicked then what we need to do okay so first of all we may need to make it the async and after the async what i am going to write here i am going to write here the await and i need to register one operation our one background task so i have the two option here the basic option that i have told you here i think so i have removed this one so i can perform here the two operations the first one is the register one-off task and then register periodic task okay so product ask what it will do uh product task will do you can see here scheduling a product task that will run every provided frequency a unique name is required so only one task can be registered the task name is the value that will be returned in the background task handler is not required okay so but one thing to remember that default timer is a 15 minute if not provided okay so a frequency has a minimum of 15 minutes so if you provide less than 15 minutes it will consider as a 15 minute okay so first of all it will accept the one unique name so you can get any unique identifier so if i just write here um the date time uh date time dot now dot seconds okay so this could be the unique identifier but let's make it not right here so let me write um here as a unique identifier for s equal to date time oh sorry i think so day time dot now now dot seconds so this could be okay so now let me write here the unique id so this unique id will be used every time whenever the new register will be implemented it says that uh it accept the the argument type int cannot be assigned to the parameter type of string so let's convert into this to string so just write here the two string okay now the task name uh what operation that we need to perform okay so the task name could be the uh the task name like whatever the task that we are going to perform like i just put here this this task name so this task name i need to call here okay so if i just write here the task simple task so now i need to uh i need to okay um execute task and this task will called here okay so this one should be here now this task have some whenever we click on this function it will register the task and we can also task the like the other operations that we can perform is the frequency is initial delay so let's talk about the frequency so let's talk about the frequency here and let's talk about the seconds like 15 seconds like okay so this is that the frequency that we can follow the other we can talk about as a average we can talk about the work manager and the work manager should as a register one of tasks okay so it is also accept the unique id and the task name so i will task our task name should be the task okay and the other operation that i can perform here the initial delay i think so let me see uh there's a backup policy there's a initial delay a lot of things that we can perform so let's call here the initial delay okay so initial delay should be the duration and it should be the i just write it a second i just write here the minutes i just write here the hours so for now let's talk about the seconds okay and we can also perform some constraints here and our constraints could be you can see the constraints as you will see the constraints here this the constraints okay so we can perform some constraints also uh the constraint could be the network type you can see there are constraints type network type required battery not low when battery will not be low our our this work will not be performed require charging uh like when our charging will be uh whenever we will be on the charging then this message or this perform this operation will be performed then if the required storage if storage will not be low then this operation will be performed so these are the things that we can perform okay so if i just porting here putting here if the network type dot connected if our network type will be connected then this operation will be performed okay i hope so you understand till now now let's close this project this this app and run it again because we are working on the main thread and whenever we will work on the method we have to close our application so we are running it again and let's see [Music] so we have run our application and let's see when we click on this schedule button and our this function will be called uh this function register one of the tasks will be performed it will show the schedule okay it will show the print task after the 10 seconds this function should be run so let's click on this shareable task uh okay uh it says work result success and you will see after 10 seconds high is printed out okay and you will also see one uh this now we will see the result is success okay this message will occur when i am going to uh write i i write here that is debug more true if you just comment out or you can write here that is debug mode false then you will not see this notification okay i hope so understand we will perform some more complex tasks in the future uh in the next lessons so if you are new here just subscribe my channel
Info
Channel: Just Flutter
Views: 19,038
Rating: undefined out of 5
Keywords: flutter background task, android background task, Background Services in Flutter, Background Fetch, Foreground services in flutter, running background processes, flutter_background_service, workmanager
Id: J9Tt4WHtRAg
Channel Id: undefined
Length: 17min 5sec (1025 seconds)
Published: Wed Jul 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.