I am developing a Flutter app for academic purposes in university. Now, I need to run a sort of "always on" background service in order to use Bluetooth and most importantly to get periodic updates from external REST APIs.
I've already checked those two libraries from pub.dev (this and this) without finding the perfect solution that works out-of-the-box (as expected...). I also saw something native with Kotlin (here). What I would to know is what is the best option in order to achieve my goals (in terms of best practices, completeness and with simplicity in mind).
Have a look at the following image for a more schematic view:
Please note: This timer only runs when the app is running - it does not schedule anything to be run in the background using OS-specific scheduling mechanisms, nor does it attempt to wake the device or app.
Flutter background_fetch Background Fetch is a very simple plugin which will awaken an app in the background about every 15 minutes, providing a short period of background running-time. This plugin will execute your provided callbackFn whenever a background-fetch event occurs.
Can I run Dart code in the background of an Flutter app?
Yes, you can run Dart code in a background process on both iOS and Android. For more information, see the Medium article Executing Dart in the Background with Flutter Plugins and Geofencing.
Credit to Raouf Rahiche
For this kind of things you have to rely on native mechanisms.
The most efficient way to deal with APIs in an "always existent" background component is to implement a push notification service. Using that kind of tech a remote server is capable of starting communication with your app, awaken it and then perform whatever task needs to be performed.
Also, in Android, you have foreground services which will run even if the app is closed.
You might want to have a look at flutter_background_service package. It helps executing Dart code in the background.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With