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);
  }
flutter
  • 6,188
  • 9
  • 45
  • 78

1 Answers1

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);
                }
bluenile
  • 5,673
  • 3
  • 16
  • 29
  • It's not a future. It has a duration property. – flutter Nov 14 '20 at 07: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 '20 at 08:18