I am trying to show the home page if the user already logged in else I need to show the login page.
I have tried like below:
void main() async{
bool isLoggedIn = false;
SharedPreferences prefs = await SharedPreferences.getInstance();
bool isLoggedInValueAvailable = prefs.containsKey('isLoggedIn');
if(isLoggedInValueAvailable)
{
isLoggedIn = prefs.getBool('isLoggedIn')?? false;
}
else
{
isLoggedIn = false;
}
runApp(MyApp(isLoggedIn: isLoggedIn));
}
class MyApp extends StatelessWidget {
final bool isLoggedIn;
MyApp({required this.isLoggedIn});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: isLoggedIn ? const Home() : const LoginDemo(),
debugShowCheckedModeBanner: false,
routes: {
'/home': (context) => Home(),
'/sign_up': (context) => SignUp(),
},
);
}
}
But when I run this a white screen is only showing, not redirecting to login or home page. No exception or errors are showing the console.
>Solution :
void main() async {
WidgetsFlutterBinding.ensureInitialized(); // <== add this line
...
}