How to Fluttter App run continuously fetch data after every 10 sec in background when app is closed/termineted or screen off

Viewed 78

How to Fluttter App run continuously in background when app is closed/termineted.i have to fetch loaction contiuously when app is terminated/closed or screen is locked.

1 Answers

Try work manager

https://pub.dev/packages/workmanager

Create a root level method and use that method to create a background process.

void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {
    print("Native called background task: $backgroundTask"); //simpleTask will be emitted here.
    return Future.value(true);
  });
}

void main() {
  Workmanager().initialize(
    callbackDispatcher, // The top level function, aka callbackDispatcher
    isInDebugMode: true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
  );
  Workmanager().registerOneOffTask("task-identifier", "simpleTask");
  runApp(MyApp());
}
Related