I'm building a Flutter app where some screens can be shown to anonymous users, and other screens require a user to be logged in.
For the authenticated screens, they should automatically navigate to (push) a login screen if the user is not logged in. A user session can expire at any time and if the user is viewing one of these authenticated screens then the login screen should be shown immediately at that time.
In Flutter, how can I achieve this notion of authenticated screens that automatically navigate to/from a login screen when the user is not authenticated?
Flutter Login ScreenFirst there is a widget for the company/organization/app name. Then about the screen itself, Sign in. Now, we have two text fields, user name and password, to get login/sign-in credentials from user. Then we have a TextButton widget for the Forgot Password.
Currently, there's nothing that flutter do with Authentification and Authentified routes. The problem is that dart:mirror is disabled, which prevents from doing a more automated solution.
You could try to :
MaterialApp
's routes
propertyMaterialApp
's onGenerateRoute
propertyAnd make sure inside onGenerateRoute
that the user is logged. If he is, build that route. If not, build the Login
route with the original destination passed as parameter (to later redirect to that page)
A code generator may be a good solution too ; although more complex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With