I’m developing a Flutter application for Windows Desktop and I’ve encountered a problem. I need my app to sync data to the server even when the app is closed. This is crucial for my application as it needs to maintain up-to-date data on the server at all times.
I’ve been searching for a solution but haven’t found a suitable one yet. Is there a specific package or technique that could help me achieve this functionality? Any guidance or suggestions would be greatly appreciated.
Thank you in advance!
I’ve tried using the connectivity_plus package, but it doesn’t seem to provide the functionality I need when the app is closed.
So, as requested, here's how I created a Windows service written in Flutter/Dart. Mostly, I used NSSM to wrap my Flutter-Windows app, because, on Flutter part, you can pretty much write your app as usual. The only thing to look out for is the lack of a GUI: since it's a service in background, you probably don't want a window visible on the desktop.
This is MyApp class:
I could not use the usual
bitsdojo_windowpackage because there was some issue at the time with itshidemethod (I opened an issue here, but it's still pending), so I had to resort to work around it by creating a GUI of 1x1 transparent pixel. For some reasons that I have not investigated, when running the service from Android Studio, you still get a blacked-out window as a GUI: this does not happen with the final exe file.The service job is done directly in the
mainmethod: in my case, I set up a cron job to run some methods at different time of the day, but you could simply set up one or moreTimers to synchronize the data as frequently as you need.Once the code is ready, you just need to build the app as normal Windows application (just check the Flutter tutorial on how to do it), then follow the NSSM instructions to wrap your app and make it run as a service: the simplest way to do it is to run
nssm install "My service" your_flutter.exe -sfrom the command line, on a folder where you have both nssm.exe and the Flutter exe file. I made a .bat file to handle the service installation, here it is: