auto_route_library icon indicating copy to clipboard operation
auto_route_library copied to clipboard

[BUG] @RoutePage<bool>() cannot generated

Open cogivn opened this issue 10 months ago • 2 comments

Hi, I configured the @RoutePage<bool>() to retrieve a specific value but encountered crashes.

@RoutePage<bool>()
class LoginPage extends StatelessWidget {
  // Your code here
}
abstract class _$AppRouter extends RootStackRouter {
  // ignore: unused_element
  _$AppRouter({super.navigatorKey});

  @override
  final Map<String, PageFactory> pagesMap = {
    LoginRoute.name: (routeData) {
      return AutoRoutePage<dynamic>(
        routeData: routeData,
        child: const LoginPage(),
      );
    },
    HomeRoute.name: (routeData) {
      return AutoRoutePage<dynamic>(
        routeData: routeData,
        child: const HomePage(),
      );
    },
    SplashRoute.name: (routeData) {
      return AutoRoutePage<dynamic>(
        routeData: routeData,
        child: const SplashPage(),
      );
    },
    TestRoute.name: (routeData) {
      return AutoRoutePage<dynamic>(
        routeData: routeData,
        child: const TestPage(),
      );
    },
  };
}
...
}
class AuthGuard extends AutoRouteGuard {
  @override
  void onNavigation(NavigationResolver resolver, StackRouter router) async {
    // the navigation is paused until resolver.next() is called with either
    // true to resume/continue navigation or false to abort navigation
    final authRepository = getIt<IAuthRepository>();
    final isLogin = await authRepository.isLoggedIn();
    if (isLogin) {
      resolver.next(true);
    } else {
      // else we navigate to the Login page so we get authenticated

      // tip: use resolver.redirect to have the redirected route
      // automatically removed from the stack when the resolver is completed
      resolver.redirect<bool>(const LoginRoute()).then((didLogin) {
        resolver.next(didLogin);
      });
    }
  }
}

return the result:

BlocConsumer<AuthCubit, AuthState>(
          listener: (context, state) {
            state.whenOrNull(
              authenticated: (user) => context.popRoute<bool>(true),
              error: (error) => error.whenOrNull(
                other: (message) => _showError(message),
              ),
            );
          },

and the error:

E/flutter (16099): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'AutoRoutePage<dynamic>' is not a subtype of type 'AutoRoutePage<bool>' in type cast
E/flutter (16099): #0      StackRouter._addEntry (package:auto_route/src/router/controller/routing_controller.dart:1552:18)
E/flutter (16099): #1      StackRouter._push (package:auto_route/src/router/controller/routing_controller.dart:1251:14)
E/flutter (16099): <asynchronous suspension>

cogivn avatar Oct 24 '23 17:10 cogivn

I came across the same issue, it seems like there's a bug when using result value in nested routes. I solved mine by moving the route to the root level, but it might not be ideal for your case. This is a duplicate issue of #1732

Lamorak avatar Dec 11 '23 10:12 Lamorak

This approach work fine on flutter web but not on android app

CoderJerry avatar Dec 20 '23 12:12 CoderJerry