I am facing this issue and didn’t know what to do here
showSnackbar(
context,
text: StatusCodes.absenceCancelResponse[statusCode],
color: Colors.green,
);
class StatusCode
{
static Map<int, String> absenceCancelResponse = {
0: "Absence Request Canceled Successfully",
1: "Error Cancelling Absence Request",
};
}
Everything working fine but I am annoyed by this warning
Do not use BuildContexts across async gaps
This has been answered a numerous times here. Just search for it: https://stackoverflow.com/search?q=BuildContexts+across+async+gaps
In short. Use a StatefulWidget. Before the warning, check if the widget is mounted. Do this using if (mounted) { ... }
Put the lines where you get the warning inside the curly brackets { }
A StatefulWidget
is no longer necessary. In a StatelessWidget
you can now use context.mounted