upendra
upendra

Reputation: 21

How to Sync Data to Server from a Flutter Windows App Even When the App is Closed?

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.

Upvotes: 0

Views: 289

Answers (1)

il_boga
il_boga

Reputation: 1513

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:

class MyApp extends StatelessWidget {
  final ini_cfg.Config ini;
  const MyApp(this.ini, {super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MyApp',
      home: SizedBox(width: 1, height: 1, child: Container(color: Colors.transparent)),
    );
  }
}

I could not use the usual bitsdojo_window package because there was some issue at the time with its hide method (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 main method: 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 more Timers 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 -s from 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:

@ECHO OFF
ECHO Installing service...
SET serviceName="My custom service"

nssm status %serviceName% 

if %ERRORLEVEL% EQU 0 (
    echo Service already installed
    nssm restart %serviceName%
    echo ERR: %errorlevel%
    if %ERRORLEVEL% NEQ 0 (
        echo Can't restart the service. Try manually or reboot.
        PAUSE
        EXIT /B 0
    )
    PAUSE
    EXIT /B 0
)

nssm install %serviceName% your_flutter.exe -s
if %ERRORLEVEL% NEQ 0 (
    ECHO Installation failed
    PAUSE
    EXIT /B -1
)
ECHO Service installed

nssm set %serviceName% AppDirectory "%cd%"
if %ERRORLEVEL% NEQ 0 (
    ECHO Can't set execution folder
    PAUSE
    EXIT /B -2
)

nssm set %serviceName% Description "My custom service description"

nssm set %serviceName% Start SERVICE_AUTO_START
if %ERRORLEVEL% NEQ 0 (
    ECHO Can't set autostart
    PAUSE
    EXIT  /B -3
)

nssm set %serviceName% AppStdOut "%cd%\service.log"
if %ERRORLEVEL% NEQ 0 (
    ECHO Can't set log file for stdout
    PAUSE
    EXIT  /B -10
)

nssm set %serviceName% AppStdErr "%cd%\service.log"
if %ERRORLEVEL% NEQ 0 (
    ECHO Can't set log file for stderr
    PAUSE
    EXIT  /B -11
)

nssm start %serviceName%
if %ERRORLEVEL% NEQ 0 (
    ECHO Can't start the service
    PAUSE
    EXIT  /B -100
)

ECHO Successfully completed
PAUSE
EXIT /B 0

Upvotes: 0

Related Questions