I have two screens A and B. I am currently on screen A and I call a function that displays a dialog after 10 seconds. The problem is that when I navigate to screen B before the dialog appears, the dialog appears on screen B instead of A, even though I have used _scaffoldKey.currentContext.
showDialog(
context: _scaffoldKey.currentContext!,
builder: (ctx) {
return Widget();
},
)
i use Navigator.of(context).pushNamed('/screenB) to navigate to screen B.
How can I ensure that the dialog only appears on screen A?
Hung.
The issue may be due to the fact that you are passing in the current
contextof_scaffoldKeyat the time theshowDialogmethod is called. However, if younavigateto screen B before the dialog appears, then thecurrentcontextmay no longer refer to screen A.Instead of explicitly relying on
_scaffoldKey.currentContext, One way to do is by wrapping the widget withBuilderHope it help, Also the basic definition of
contextto clear your mind further,