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
I would try to create StreamSubscription what listen on authorisation state changes and navigate to desired screen depending on emitted state.
Use
redirect
in yourGoRoute
definition.