3

I am making an app with flutter. I want to store data after 24 hours and update UI in app. I try with Timer.periodic() but it does not count the time when app is close. It only works when the application is open.

Is it possible to execute a function after a specific time even if the app is closed?

Here is my current code:

void callbackDispatcher() async{
  Workmanager().executeTask((task, inputData) {
    switch(sdDaily){
      case 'StoreDataDaily':
       storeData.storeDailyData();
        break;
      default:
    }
    return Future.value(true);
  });
}
void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  Directory directory = await path_provider.getApplicationDocumentsDirectory();
  print(directory.path);
  Hive.init(directory.path);
  await Hive.initFlutter(directory.path);
  Hive.registerAdapter(UserAdapter());
  Hive.registerAdapter(WaterAdapter());
  Hive.registerAdapter(WeekAdapter());
  Get.put(UserController());
  Get.put(WaterController());
  await Hive.openBox<User>('data');
  await Hive.openBox<Water>('water_data');
  await Hive.openBox<Week>('week_data');
  await notificationPlugin.showNotification();
  await Workmanager().initialize(callbackDispatcher, isInDebugMode: true);
  var uniqueId = DateTime.now().second.toString();
  var userBox = Hive.box<User>('data');
  if(userBox.get(0)?.status == 1){
    await Workmanager().registerOneOffTask(uniqueId, sdDaily,);
  }

  runApp(const MyApp());
}

padaleiana
  • 955
  • 1
  • 14
  • 23
Sania Developer
  • 130
  • 1
  • 9

2 Answers2

0

You can use : flutter_background_service. to execute background services and it'll also help you sending a custom notification when you are actually going to store that data later.

Risheek Mittal
  • 1,077
  • 2
  • 18
0

You can use firebase cloud funcitons to do schedule tasks or whatever you want to do even if app is closed or killed.