Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter: Issue with showing home page if the user already logged in else show the login page

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

void main() async {
  WidgetsFlutterBinding.ensureInitialized(); // <== add this line
  ...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading