I have BottomNavBar with 4 items, i want to change last item Login/Registration screen to profile screen when user is authorized, i check auth with sharedPreferencies plugin, I can't create a condition in my code that will change screens and titles my code:
@override
void didChangeDependencies() async {
await getToken();
print(val);
}
late final String? val;
getToken() async {
SharedPreferences pref = await SharedPreferences.getInstance();
Constants.USER_TOKEN = pref.getString('login') ?? "";
setState(() {
val = Constants.USER_TOKEN;
});
}
late int currentIndex;
final List<String> screenTitles = ['Главная', 'Каталог', 'Корзина', 'Войти'];
final screens = [
HomePage(),
CatalogPage(),
ShoppingBag(),
Login(),
];
//it should look like this
// if(val != null && val != '') {
// return ProfilePage();
// } else {
// return Login();
// }
CodePudding user response:
Change your getToken
to this:
getToken() async {
SharedPreferences pref = await SharedPreferences.getInstance();
Constants.USER_TOKEN = pref.getString('login') ?? "";
val = Constants.USER_TOKEN;
screens = [
HomePage(),
CatalogPage(),
ShoppingBag(),
val != null && val != '' ? ProfilePage(): Login(),
];
setState(() {});
}