Reputation: 827
I'm having a mobile app with many screen (sreen A, screen B, screen C ...)
The requirement in my application to include two types of users. One type of user will have access to all screen and the second type user (not loggin) will only have access to Screen A , Screen B. How can I do that ?
My idea is store token after user loggin by SharedPreferences
. And check the token is null
or not
. If not null
, user can access all screen. But I don't know where to put this code ? At the main.dart
or each screen ?
getToken() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String token = sharedPreferences.getString("token");
//print(token);
return token;
}
//if token != null { ..can access all Screen } else { ... }
Upvotes: 1
Views: 241
Reputation: 2171
you have two options:
global.dart
file and save getToken()
and also a variable for the storedvalue of token (so only check for the value of sharedPref once) in there. then you can access it as follow:import './globals.dart' as globals;
globals.accessToken // for getting token value
//or
globals.getToken() // for getting token directly from sharedPref
provider
Upvotes: 0
Reputation: 12373
Wrap your home screen in a Futurebuilder. use getToken()
as the future for this future builder. Based on the data returned from your function, return the screen you want.
Upvotes: 1