Currently, I am checking on SplashScreen
whether token exist or not, on that basis app navigates to either SignIn
or Home
.
On Home
when I turn off the internet, it shows a dialog that there is no internet connection. By turning it on, the dialog disappears.
The problem is when I close the app and turn off the internet and then open the app it navigates to SignIn
. I expect it to be navigated to Home
as token is already there.
It works normally when I turn on the internet and visit the app, it navigates to Home
directly.
Need an idea to resolve this issue.
Here’s the SplashScreen
code for navigating the app.
Future<void> navigate() async {
final bearer = await ref.read(bearerTokenProvider.future); // stored in local db
if (!mounted) return;
GoRouter.of(context)
.pushReplacement(bearer != null ? RoutePaths.home : RoutePaths.login);
}
3
Answers
Did you cache your token when it is validated? If yes then you can just check your cached token while navigating from MaterialApp.
@Aiman I bet the bearerTokenProvider code is the problem. Either:
Please provide the code where you do both
To ensure that the app navigates to the Home screen when the token exists, regardless of the internet connection status, you can modify the SplashScreen code to check for the presence of the token and navigate accordingly. you can use shared_preferences for this. SharedPreferences is a key-value storage mechanism in Flutter for persistently storing small amounts of data.
First, make the
userSignIn
function so that after a user signIn, the token is stored in the cache. I don’t know what your code is like, make it as you want.create the
singUp
function in the same and null the token when usersignOut
to navigate pages, make it like this: