title: how to keep user login in flutter using sharedpreferences
body: <p>this question have already asked in stack overflow, but I still get problem in my code.. right now, I have Splash Screen, Login and Home Screen... after user login they will navigate to Home Screen, and whenever they close the app before click logout, I want to redirect them to Splash Screen and then Home Screen, is there a way to do that... I have tried using SharedPreferences to save and get a key but before I click logout (just click back button), my app always redirect to my Login screen not my Home screen, is there something that I miss? This is my Main.dart code</p>
  
  <pre><code>void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: SplashScreen(),
      );
    }
  }
  </code></pre>
  
  <p>This is my splash screen</p>
  
  <pre><code>class SplashScreenState extends State&lt;SplashScreen&gt; {
    Future check() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String _key = (prefs.getString('key') ?? false);
      if (_key==null) {
      Navigator.of(context).pushReplacement(
          MaterialPageRoute(builder: (context) =&gt; new LoginScreen()));
      } else {
        Navigator.of(context).pushReplacement(
          MaterialPageRoute(builder: (context) =&gt; new BottomTab()));
      }
    }
  
    @override
    void initState() {
      super.initState();
      new Timer(new Duration(seconds: 3), () {
        check();
      });
    }
  
  
    @override
    Widget build(BuildContext context) {...}
  </code></pre>
  
  <p>and in my Login screen I have added this code to save my key and then navigate it to Home Screen (BottomTab)</p>
  
  <pre><code>SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setString('key', myKey);
  
  Navigator.pushReplacement(context,
                  MaterialPageRoute(builder: (BuildContext ctx) =&gt; BottomTab()));
  </code></pre>
  
  <p>and this is my Home Screen that contains Bottom Tab</p>
  
  <pre><code>class _BottomTab extends State&lt;BottomTab&gt; {
  Future&lt;bool&gt; _back() {
      return showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              content: Text('exit?'),
              actions: &lt;Widget&gt;[
                FlatButton(
                  child: Text('ok'),
                  onPressed: () {
                    Navigator.of(context).pop(true);
                  },
                ),
  FlatButton(
                  child: Text('no'),
                  onPressed: () {
                    Navigator.of(context).pop(false);
                  },
                ),
              ],
            );
          });
    }
    int _selectedIndex = 0;
  
    _onTap(int index) {
      setState(() =&gt; _myindex = index);
    }
  
    final List&lt;Widget&gt; pages = [
      HomeScreen(),
      ProfileScreen(), 
    ];
  
    final PageStorageBucket bucket = PageStorageBucket();
  
    Widget _myNavigation(int selectedIndex) =&gt; BottomNavigationBar(
          onTap: _onTap,
          currentIndex: myindex,
          type: BottomNavigationBarType.fixed,
          items: const &lt;MyTab&gt;[
            MyTab(icon: Icon(Icons.home), title: Text('Home')),
            MyTab(icon: Icon(Icons.person), title: Text('Profile')),
          ],
        );
  
    @override
    Widget build(BuildContext context) {
      return FutureProvider&lt;String&gt;(
        create: (context) async {
          final prefs = await SharedPreferences.getInstance();
          return prefs.getString("username");
        },
        child: Scaffold(
          key: scaffoldKey,
          body: WillPopScope(
            onWillPop: _back,
            child: PageStorage(
              child: pages[_selectedIndex],
              bucket: bucket,
            ),
          ),
          bottomNavigationBar: _bottomNavigationBar(_selectedIndex),
        ),
      );
    }
  }
  </code></pre>