Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It's possible run code like a "background service" when the app is closed using flutter?

Tags:

flutter

I need to run a dart code for each 30s even if the application is closed. Is it possible?

Some workarounds suguested is use AlarmManager for Android, but, I not found solution for iOS.

like image 889
Mauro Alexandre Avatar asked May 08 '19 22:05

Mauro Alexandre


People also ask

Can I hide Flutter app contents when the app is in background?

Yes, and I override the didChangeAppLifecycleState method. And in that you cleared the state or navigated to another screen? Well, now I know that you are not the only one facing this issue. And the android docs say that for this functionality resort to native code as much as possible.


2 Answers

There's no good article on how to implement an isolate to work when the app is closed without running some native code. In total, I have spent 2 full days trying to find a package for this, but nothing exists that works. Flutter_Isolate doesn't run when app is killed and Flutter Workmanager can only do print statements but can't take in any functions or any method call. Ultimately pathetic and very annoying. The only solution seems is to write native code to handle background tasks when app is closed. Shame on flutter.

like image 78
Axes Grinds Avatar answered Oct 18 '22 02:10

Axes Grinds


Yes, it is possible. You can run Flutter in background.

In Flutter, you can execute Dart code in the background.

The mechanism for this feature involves setting up an isolate. Isolates are Dart’s model for multithreading, though an isolate differs from a conventional thread in that it doesn’t share memory with the main program. You’ll set up your isolate for background execution using callbacks and a callback dispatcher.

Source: Background processes

like image 21
Rubens Melo Avatar answered Oct 18 '22 01:10

Rubens Melo