Try wrapping your Navigator
call:
Navigator.of(context).pushNamed("login");
in a callback that is scheduled with addPostFrameCallback
:
SchedulerBinding.instance.addPostFrameCallback((_) {
Navigator.of(context).pushNamed("login");
});
You’ll need this import at the top of your file:
import 'package:flutter/scheduler.dart';
As an alternative, consider if you could just have MyHomePage
‘s build()
method return a LoginPage
instead of a Scaffold
if the user isn’t logged in. That will probably interact better with the back button since you don’t want the user backing out of the login dialog before they are done logging in.