0

I want to create Authentication Flow with Gorouter and SharedPreferences. If the user is not logged in, it should go to LoginScreen else to MainLayout Screen. But in my case, context.goNamed does not work. There is no error show in my console

Here is the logic in my splash screen

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      checkLoginState();
    });
  }
  void checkLoginState() {
    print("in");
    if (locator.get<AppPreferences>().isLoggedIn()) {
      print("in questMainLayoutRoute");
      context.goNamed(AppRouteName.questMainLayoutRoute);
    } else {
      print("in loginScreen");
      context.goNamed(AppRouteName.loginScreen);
    }
  }
 

  @override
  void dispose() {
    super.dispose();
  }

These are me routes'

abstract class AppRoute {
  static GoRouter routes = GoRouter(
    redirect: (context, state) => AppRouteName.splashScreen,
    routes: [
      GoRoute(
        name: AppRouteName.splashScreen,
        path: AppRouteName.splashScreen,
        builder: (context, state) => SplashScreen(),
      ),
     
      GoRoute(
        name: AppRouteName.loginScreen,
        path: AppRouteName.loginScreen,
        builder: (context, state) => LoginUI(),
      ),
      GoRoute(
        name: AppRouteName.questMainLayoutRoute,
        path: AppRouteName.questMainLayoutRoute,
        builder: (context, state) => QuestMainLayout(),
      ),
}

Authentication Flow

muttonlamb
  • 6,341
  • 3
  • 26
  • 35

0 Answers0