I have an authenticated Flutter page that redirects the user to the login page if they are not logged in. The page looks like the following:
@override
void initState() {
super.initState();
if (!loggedIn) {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => LoginPage()),
(Route<dynamic> route) => false,
);
}
}
But I am getting the following error:
Exception has occurred.
FlutterError (setState() or markNeedsBuild() called during build.
This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was:
Overlay-[LabeledGlobalKey<OverlayState>#f9d7a]
The widget which was currently being built when the offending call was made was:
_SelectionKeepAlive)
What is the way to redirect a user to another page depending on a condition?
2
Answers
you can use
SchedulerBinding
which does the same:You could just always use Future.delayed with navigation in initState, even with the duration set to zero, the callback is initiated after the widget itself is initialized.