0

I'm uploading an image using the bloc pattern. I have a bloc listener that listens for for the state UploadSuccess. I'd like to call two methods in the listener, One to pop the route and another to show a snackbar:

if (state is UpLoadSuccess) {
                      RioHelpers.showSuccessFlushBar(context, "Document Uploaded!");
                      Navigator.pop(context);
                    }

Calling one of the methods works correctly. I can either pop the route or show the snackbar but not both. When both the methods are present nothing happens. How can I call both the methods?

here is the flushbar:

static void showSuccessFlushBar(BuildContext context, String title) {
    Flushbar(
      duration: Duration(seconds: 3),
      icon: Icon(
        Icons.check,
        color: Colors.white,
      ),
      backgroundColor: RioColours.snackBarSuccess,
      message: title,
      flushbarStyle: FlushbarStyle.FLOATING,
      margin: EdgeInsets.all(8),
      borderRadius: 8,
    )..show(context);
  }

1 Answer 1

1

If RioHelpers.showSuccessFlushBar is a future and Snackbar disappears in a few moments then you may try awaiting it

if (state is UpLoadSuccess) {
                  RioHelpers.showSuccessFlushBar(context, "Document Uploaded!");
                  await Future.delayed(Duration(seconds: 1));
                  Navigator.pop(context);
                }
2
  • It's not a future. It has a duration property.
    – flutter
    Nov 14, 2020 at 7:50
  • What seems to be happening is Navigator.pop(context) is popping the Snackbar immediately. Therefore it seems nothing is happening. You may try adding await Future.delayed(Duration(seconds: NNN)); where NNN is number of seconds to wait. You'll also need to add async to function.
    – bluenile
    Nov 14, 2020 at 8:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.