Authentication, sharedpreference flutter

Viewed 25

i created a login page but have a problem when i firt start on my phone. i use sharedpreference to save "isLoggedIn" (true or false), if is true they are logged if is false they aren t logged.

So when i start first time, isloggedIn start with true, but i want he start with false for get register page.

I don t kwon if is possible change start value of "isLoggedIn", or have outer solution for this case

this is the code i use to see what page should it start.

@override
  void initState() {
    super.initState();
    verificarToken().then((value) {
      if (value == 'true') {
        Navigator.pushReplacement(context,
            MaterialPageRoute(builder: (context) => const LoginSuccess()));
      } else {
        Navigator.pushReplacement(
            context, MaterialPageRoute(builder: (context) => const Login()));
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(child: Text('Loading...')),
    );
  }
}

Future<String> verificarToken() async {
  SharedPreferences sharedPreference = await SharedPreferences.getInstance();
  if (sharedPreference.getBool('isLoggedIn') != false) {
    return 'true';
  } else {
    return 'false';
  }

After first run this works well, the problem is only at the beginning

0 Answers
Related