How to do a long-running foreground service in Flutter?

Viewed 587

I have a service that is based on a long-running HTTP server process. I want it to work on Linux/Android/Windows, so thought I might be able to write it in Dart and do the UI in Flutter.

This works for the basic case of starting the server directly from the UI. However, I need the HTTP server to keep running even if the app is closed/killed by Android. As I understand it, this requires the use of an Android foreground service with a permanent notification. That's fine, but unfortunately from what I can tell this requires writing a Flutter plugin to handle the Android-specific bits.

I'm trying to figure out how to connect all the pieces together. My Flutter code is in Dart, and my service is in Dart, but it requires Android Java/Kotlin in between. Is there some way to hand the Android plugin a Dart function to run, after it sets up all the permissions and notification? Or am I going to have to use the NDK to run the Dart code from the Android plugin? If that's the case I would probably just write the server in something other than Dart, ie Rust or Go.

I found a couple examples, here and here, but they both seem based on asynchronous message passing. I need my HTTP server to be constantly running.

0 Answers
Related