title: Flutter : Keep User Login with SharedPreferences
body: <p>i have some problem with <a href="https://pub.dev/packages/shared_preferences" rel="nofollow noreferrer">Shared Preferences</a>. How to keep users Login though app close ? 
  <p>
  I already storing username with this code :</p>
  
  <pre><code>setState(() {
          preferences.setString('token', dataUser[0]['username']);
          Navigator.of(context).pushAndRemoveUntil(
              PageTransition(
                  type: PageTransitionType.rightToLeftWithFade,
                  child: HomePage()),
              (Route&lt;dynamic&gt; route) =&gt; false);
        });
  </code></pre>
  
  <p><p> When i clik button login , it's working go to HomePage. To ensure the username is storing/not, I make function to get my token and fetch that token.
  <p>My token fetch what i expect with this code : </p>
  
  <pre><code>String checkPref;
    getPrefIdUser() async {
      SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
      setState(() {
        checkPref = sharedPreferences.getString('token');
        if (checkPref == null) {
          Navigator.of(context).pushAndRemoveUntil(
              PageTransition(
                  type: PageTransitionType.leftToRightWithFade,
                  child: LoginPage()),
              (Route&lt;dynamic&gt; route) =&gt; false);
        } else {
          // Navigator.of(context).pushAndRemoveUntil(
          //     PageTransition(
          //         type: PageTransitionType.leftToRightWithFade,
          //         child: HomePage()),
          //     (Route&lt;dynamic&gt; route) =&gt; false);
        }
      });
    }
  </code></pre>
  
  <p><p> I Already put <code>getPrefIdUser()</code> in initState like this in my HomePage:</p>
  
  <pre><code> @override
    void initState() {
      super.initState();
      getPrefIdUser();
    }
  
  </code></pre>
  
  <p><p>Problem is, when i restart my app (i try in real device) users back to Login page again. I don't know the problem it's.
  <p> Can you help me with this problem . Thanks.</p>
  
  <blockquote>
    <p>It's My Button Login Function </p>
  </blockquote>
  
  <pre><code>void _loginMahasiswa() async {
      SharedPreferences preferences = await SharedPreferences.getInstance();
      final body = {"email": _txtEmail.text, "password": _txtPassword.text};
      var loginMahasiswa = await api.loginMahasiswa(body);
      String message = loginMahasiswa['message'];
      var dataUser = loginMahasiswa['data'];
  
      if (loginMahasiswa['status'] == true) {
  
        setState(() {
          preferences.setString('token', dataUser[0]['username']);
          Navigator.of(context).pushAndRemoveUntil(
              PageTransition(
                  type: PageTransitionType.rightToLeftWithFade,
                  child: HomePage()),
              (Route&lt;dynamic&gt; route) =&gt; false);
        });
      } else {
        print(message);
        _showSnackBar(context, message, Colors.red);
      }
    }
  </code></pre>