Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter background services while uploading file to api ,I want to update the progress using streamcontroller to update the UI, But it's not working.how can we achieve this ? #472

Open
ramaatamai opened this issue Aug 21, 2024 · 2 comments

Comments

@ramaatamai
Copy link

No description provided.

@ayoub00400
Copy link

exmaple :
// service side :

progressStream.listen((value) {
data.addAll(value);
double progressPercentage = (data.length * 100) / contentLength;

//this line solve your progress refresh in ui :
service.invoke('refresh', {"0": progressPercentage});
_showProgressNotification(service, progressPercentage, productData['productName']);
}).onDone(() async {
var fileStorePath = "/storage/emulated/0/erp/";
var storeDir = Directory(fileStorePath);
if (!storeDir.existsSync()) {
storeDir.createSync(recursive: true);
}

    final file = File(
      '$fileStorePath/${productData['productName']}${mimeToExtension[response.headers['content-type']]}',
    );
    await file.writeAsBytes(data, mode: FileMode.write);

    await Future.delayed(
      const Duration(seconds: 1),
      () {
        service.stopSelf();
      },
    );
  });

//Logic side :

// this line to listen to progress value in ui :
FileDownloaderService.flutterBgInstance.on('refresh')
..listen((event) {
if (event!['0'] == 100) {
emit(ProductDownloaded(message: "DownLoad FInished"));
} else {
emit(ProductDownLoading(productId: productData.id, progress: (event['0'] as double).toInt()));
}
});

i hope you find this helpful

@ramaatamai
Copy link
Author

@ayoub00400 final service = FlutterBackgroundService();
FlutterBackgroundService().invoke("setAsBackground");

Future.delayed(Duration(seconds: 2), () async {
  service.startService();
service.invoke('startTaskWithJson', json);
});

When i tried to start the service ,I am facing "Service already running, using existing service" Issue and its not invoke service.invoke('startTaskWithJson', json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants