auto_route_library icon indicating copy to clipboard operation
auto_route_library copied to clipboard

onNavigation guard causes Stack Overflow

Open lcardosol opened this issue 8 months ago • 0 comments

I tried to apply the same logic as said in the docs for a simple auth guard, this is my AppRouter:

@AutoRouterConfig(
  replaceInRouteName: 'View,Route',
)
class AppRouter extends _$AppRouter implements AutoRouteGuard {
  AppRouter({
    required this.authState,
  }) : super();

  final AuthStateNotifer authState;

  @override
  RouteType get defaultRouteType => const RouteType.material();

  @override
  List<AutoRoute> get routes => [
        AutoRoute(page: SignInRoute.page, meta: const {'skipAuth': true}),
        AutoRoute(page: SignUpRoute.page, meta: const {'skipAuth': true}),
        AutoRoute(page: HomeRoute.page, initial: true),
      ];

  @override
  void onNavigation(NavigationResolver resolver, StackRouter router) {
    final skipAuth = router.current.meta['skipAuth'] as bool?;
    if (authState.isLogged || skipAuth == true) {
      resolver.next(true);
    } else {
      resolver.redirect(
        SignInRoute(
          onResult: (result) => resolver.next(result),
        ),
      );
    }
  }
}

Although I don't like passing a callback like that for the SignInRoute I did just to make sure I wasn't missing anything from the tutorial, but it causes an Unhandled exception:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Stack Overflow
E/flutter ( 5584): #0      StackRouter._redirect (package:auto_route/src/router/controller/routing_controller.dart:1283:18)
E/flutter ( 5584): #1      NavigationResolver.redirect (package:auto_route/src/router/controller/auto_route_guard.dart:198:20)
E/flutter ( 5584): #2      AppRouter.onNavigation (package:manager/utils/router.dart:36:16)
E/flutter ( 5584): #3      StackRouter._canNavigate (package:auto_route/src/router/controller/routing_controller.dart:1584:13)
E/flutter ( 5584): #4      StackRouter._push (package:auto_route/src/router/controller/routing_controller.dart:1244:26)
E/flutter ( 5584): #5      StackRouter._redirect (package:auto_route/src/router/controller/routing_controller.dart:1283:18)
E/flutter ( 5584): #6      NavigationResolver.redirect (package:auto_route/src/router/controller/auto_route_guard.dart:198:20)
E/flutter ( 5584): #7      AppRouter.onNavigation (package:manager/utils/router.dart:36:16)
....

I tried to debug the onNavigation method and StackRouter.current has always a "Root" route, never one of my routes, is there something I'm missing here?

Flutter doctor:

[✓] Flutter (Channel stable, 3.16.2, on macOS 13.4 22F66 darwin-arm64, locale en-CA)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.2)
[✓] VS Code (version 1.85.1)
[✓] Connected device (3 available)
[✓] Network resources

Phone used: Pixel 3A Emulator Galaxy S22 Physical device

lcardosol avatar Dec 15 '23 10:12 lcardosol