skip to Main Content
class AuthGate extends StatelessWidget {
  const AuthGate({super.key});

  @override
  Widget build(BuildContext context) {
    final AuthService authService = AuthService();
    final userProvider = context.read<UserProvider>();

    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await authService.getUserData(context: context);

      if (userProvider.userModel.token.isNotEmpty) {
        context.go(AppRouterName.homeView.path);
      } else {
        context.go(AppRouterName.signInView.path);
      }
    });

    return const Scaffold(
      body: Center(child: CircularProgressIndicator()),
    );
  }
}

I was trying go routers with flutter.

I am also doing back-end operations with node.js.

When the application is opened, if the user has logged in before, open HomeView, if not, open signUpView.

The code above works. But I think it is a very amateur code. Thanks to those who helped.

2

Answers


  1. I would try to create StreamSubscription what listen on authorisation state changes and navigate to desired screen depending on emitted state.

    Login or Signup to reply.
  2. Use redirect in your GoRoute definition.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search