Youssef Aly
Youssef Aly

Reputation: 41

Running flutter code in the background without WorkManager

I am wondering if someone managed to get such a code running in the background in Android devices without the need to use WorkManager ? I have a colleague at work who have this running smoothly in the background (on a Samsung device), but I did not manage to get it working on my Xiaomi devices. What is the catch ?

  _checkForNewMessages() async {
     // Do some actions ... 
    Future.delayed(Duration(minutes: 2), _checkForNewMessages);
  }

Upvotes: 0

Views: 371

Answers (1)

Amir Mohammad Shams
Amir Mohammad Shams

Reputation: 176

You can use flutter_isolate https://pub.dev/packages/flutter_isolate

@pragma('vm:entry-point')
Future<int> expensiveWork(int arg) async {
  int result;
  // lots of calculations
  return result;
}

Future<int> doExpensiveWorkInBackground() async {
  return await flutterCompute(expansiveWork, arg);
}

Upvotes: 0

Related Questions