Flutter showDialog(context) in initState method

For newcomers, you have access to context in initState as others said. The only problem here in initState build function is not executed yet. So this context is kind of empty.

To solve this, you can use SchedulerBinding:

SchedulerBinding.instance.addPostFrameCallback((_) => doSomethingNextFrame(context));

addPostFrameCallback: Schedule a callback for the end of this frame.

Future.delayed(Duration.zero) will also work. But I think SchedulerBinding looks cleaner.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)