Why sometimes snackbar is failing Why sometimes snackbar is failing dart dart

Why sometimes snackbar is failing


Try to check if the widget is still mounted in the three:

void showFailedSnackBar(String s) {    if (mounted) {    SnackBar snackBar = SnackBar(      content: Text(s),      duration: Duration(seconds: 3),      backgroundColor: Theme.of(context).primaryColor,    );    ScaffoldMessenger.of(context).showSnackBar(snackBar);    }  }


In your function showSnackbar, besides the String you could also pass a BuildContext context variable, so you know which context you pass e.g.

    SnackBar snackBar = SnackBar(      content: Text(s),      duration: Duration(seconds: 3),      backgroundColor: Theme.of(context).primaryColor,    );    ScaffoldMessenger.of(context).showSnackBar(snackBar);  }

You could save the context of the scaffold that you are sure it exists in a provider in order to pass it around where you need it.