Is it possible to call two methods in a bloclistener?

Issue

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);
  }

Solution

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);
                }

Answered By – bluenile

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.