auto_route_library icon indicating copy to clipboard operation
auto_route_library copied to clipboard

[Exception] "setState() or markNeedsBuild() called during build" for pushAndPopUntil

Open coldstar96 opened this issue 2 years ago • 2 comments

When calling router.pushAndPopUntil(SomeRoute, predicate: (_) => false) in AutoRouteGuard on cold start(before any view is presented), it causes this exception.

Version auto_router: ^3.2.4

FlutterError (setState() or markNeedsBuild() called during build. 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#413a5] The widget which was currently being built when the offending call was made was: RouteDataScope)

I replaced the call with router.replaceAll to fix on my end

coldstar96 avatar Mar 27 '22 22:03 coldstar96

I tried this guard on root page but I could reproduce the issue.

  void onNavigation(NavigationResolver resolver, StackRouter router) {
    if (!isAuthenticated) {
      router.pushAndPopUntil(
        LoginRoute(onLoginResult: (_) {
          resolver.next();
          router.removeLast();
        }),
        predicate: (r) => false,
      );
    } else {
      resolver.next(true);
    }
  }

Milad-Akarie avatar Apr 09 '22 14:04 Milad-Akarie

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

github-actions[bot] avatar Oct 13 '22 08:10 github-actions[bot]