Home > Back-end >  Manage Timer for each List item in the background in Flutter
Manage Timer for each List item in the background in Flutter

Time:07-17

I am working on a feature in an flutter application. The feature is that when some entry in server (DB) occurs, I get notification through socket connection and add that item in List. There is a button on the screen which shows a dialog box on clicking and shows that list of items. We can close the box also and again open when required.

Now I want to attach Timer to each item in the List. The purpose of that timer is to delete the item from the list after 1 minute. All that functionality of removing items must keep going in the background when dialog box doesn't appear. On opening the dialog box it must shows updated List each time.

Right now I am able to add items to the list and showing them in dialog box and it is working.

I just want to find out the solution of that background running task in which items automatically remove from list after the time for that item is up.

CodePudding user response:

The easiest way to perform an action after one minute might be to just await a future like this:

Future<void> deleteAfter1Minute() async {
  await Future.delayed(const Duration(minutes: 1));
  // delete item from list
  // ... your code
  print('item deleted');
}

You can also initiate a timer which fires the callback after the given duration:

final timer = Timer(const Duration(minutes: 1), () {
  // delete item from list
  // ... your code
  print('item deleted');
  timer.cancel();
});

It might also help if you attached some code to better understand where you're coming from.

Best

CodePudding user response:

you can use the background service packages for that

flutter_background_service

that is very good package for background service

  • Related