I am using flutter 3.22.3 and Dart 3.4.4 and my editor still warns me about context … even if I check if the context is mounted.
I have this helper function showToastMessage and the async gaps:
void showToastMessage(BuildContext context, String message) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(message))
);
}
// somewere in the code an async func
void _submit async {
...
var result = await CallAPi().postData(data, '/login');
...
// check the context !!!
if (context.mounted) {
showToastMessage(context, result.message); // it still complains about the context "Don`t use BuildContext across async gaps ...."
}
}
I am checking like in the documentation for the context…. so why I am still get the warning ????
What am I missing ?
>Solution :
Now in newer version of flutter you can use mounted directly inside StatefulWidget
instead of using
if (context.mounted) {
pop(context);
}
Use
if (mounted) {
pop(context);
}
you can check the details when to use context.mounted and mounted here: use_build_context_synchronously