Home > database >  Background api call (flutter)
Background api call (flutter)

Time:06-07

I want to listen to the API call state across the screen. So, the scenario is after a certain task, an API GET will be called and while waiting for the response, the user can open another screen and after the call is finished, the user can be notified using snackbar. I have done it like this:

provider.uploadCSV(widget.event.data!.id!).then((value) {
                            if (provider.state == ResultState.HasData) {
                              provider.faceEncoding(widget.event.data!.id!).then((value) {
                                var snackBar = const SnackBar(
                                    content: TextNunito(label: "Encoding Guests Face, Please Don't Close This App", fontSize: 14, fontWeight: FontWeight.w400));
                                rootScaffoldMessengerKey.currentState?.showSnackBar(snackBar);
                                if (provider.encodingState == ResultState.HasData) {
                                  var snackBar = const SnackBar(
                                      content: TextNunito(label: "Finished Encoding Guests Face", fontSize: 14, fontWeight: FontWeight.w400));
                                  rootScaffoldMessengerKey.currentState?.showSnackBar(snackBar);
                                }
                              });
                            }
                          });

but apparently, after I close the screen, the snackbar won't show. Is there any workaround for this? Any advice will be helpful, thanks!

CodePudding user response:

You can use flutter_background_service and flutter_local_notifications packages. You will listen api and when a message coming , you will show a notification , and when user click the notification , you can show page what you want

  • Related