0

Need to run the app in an android device after killing it in a flutter. I'm using background_fetch, it works fine when app is live. but it won't run after killing the app.

 BackgroundFetch.configure(BackgroundFetchConfig(
  minimumFetchInterval: 15,
  forceAlarmManager: false,
  stopOnTerminate: false,
  startOnBoot: true,
  enableHeadless: true,
  requiresBatteryNotLow: false,
  requiresCharging: false,
  requiresStorageNotLow: false,
  requiresDeviceIdle: false,
  requiredNetworkType: NetworkType.NONE,
), _onBackgroundFetch).then((int status) {
  print('[BackgroundFetch] configure success: $status');
  setState(() {
    _status = status;
  });

}).catchError((e) {
  print('[BackgroundFetch] configure ERROR: $e');
  setState(() {
    _status = e;
  });
});
kirubha sankar
  • 150
  • 1
  • 12

2 Answers2

0

You can use WorkManager plugin to do so.

const myTask = "syncWithTheBackEnd";

void main() {
  Workmanager.initialize(callbackDispatcher);
  Workmanager.registerOneOffTask(
    "1",
    myTask, //This is the value that will be returned in the callbackDispatcher
    initialDelay: Duration(minutes: 5),
    constraints: WorkManagerConstraintConfig(
      requiresCharging: true,
      networkType: NetworkType.connected,
    ),
  );
  runApp(MyApp());
}

void callbackDispatcher() {
  Workmanager.executeTask((task) {
    switch (task) {
      case myTask:
        print("this method was called from native!");
        break;
      case Workmanager.iOSBackgroundTask:
        print("iOS background fetch delegate ran");
        break;
    }

    //Return true when the task executed successfully or not
    return Future.value(true);
  });
}

The complete article medium article

iknow
  • 8,358
  • 12
  • 41
  • 68
0

Try using isolate it will give what you want , here is the link https://pub.dev/packages/flutter_isolate

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 26 '22 at 14:04