title: Flutter google sign in stay logged in
body: <p>I have implemented the firebase auth google sign in but how do i able to stay logged in after the app is closed, i even put this line <code>await _auth.currentUser()</code> but still not working. Below is my login code.</p>
  
  <p><strong>LoginPage.dart</strong> <strong>(OLD)</strong></p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  import 'package:firebase_auth/firebase_auth.dart';
  
  //Google provider
  import 'package:google_sign_in/google_sign_in.dart';
  import 'package:flutter_facebook_login/flutter_facebook_login.dart';
  import 'package:flutter_auth_buttons/flutter_auth_buttons.dart';
  
  final FirebaseAuth _auth = FirebaseAuth.instance;
  final GoogleSignIn _googleSignIn = new GoogleSignIn();
  
  class LoginPage extends StatefulWidget {
      @override
      _LoginPageState createState() =&gt; _LoginPageState();
  }
  
  class _LoginPageState extends State&lt;LoginPage&gt; {
  String _email;
  String _password;
  GoogleSignInAccount _currentUser;
  
  @override
  void initState() {
    super.initState();
    _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) 
  {
    setState(() {
      _currentUser = account;
    });
    if (_currentUser != null) {
      //_handleGetContact();
    }
  });
  _googleSignIn.signInSilently();
  }
  
  //google sign in
  GoogleSignIn googleAuth = new GoogleSignIn();
  
  //Facebook sign in
  FacebookLogin fbLogin = new FacebookLogin();
  
  Future&lt;FirebaseUser&gt; _testSignInWithGoogle() async {
  final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  final GoogleSignInAuthentication googleAuth =
      await googleUser.authentication;
  final AuthCredential credential = GoogleAuthProvider.getCredential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );
  final FirebaseUser user = await _auth.signInWithCredential(credential);
  assert(user.email != null);
  assert(user.displayName != null);
  assert(!user.isAnonymous);
  assert(await user.getIdToken() != null);
  
  final FirebaseUser currentUser = await _auth.currentUser();
  assert(user.uid == currentUser.uid);
  
  print('Signed in as ${user.displayName}');
  Navigator.of(context).pushReplacementNamed('/homepage');
  
  return await _auth.currentUser();
  }
  
  void _facebookLogin() async {
  var facebookLogin = FacebookLogin();
  var facebookLoginResult =
      await facebookLogin.logInWithReadPermissions(['email', 
  'public_profile']);
  
   switch (facebookLoginResult.status) {
    case FacebookLoginStatus.error:
      print("Error");
      onLoginStatusChanged(false);
      break;
    case FacebookLoginStatus.cancelledByUser:
      print("CancelledByUser");
      onLoginStatusChanged(false);
      break;
    case FacebookLoginStatus.loggedIn:
      print("LoggedIn");
      onLoginStatusChanged(true);
  
      FacebookAccessToken myToken = facebookLoginResult.accessToken;
      AuthCredential credential= 
  FacebookAuthProvider.getCredential(accessToken: myToken.token);
      try {
        final FirebaseUser user = await 
  FirebaseAuth.instance.signInWithCredential(credential);
  
        //Navigator.pushReplacement(context, MaterialPageRoute(builder: 
  (context) =&gt; DiscoverPage()));
        print('Signed in as ${user.displayName}');
        Navigator.of(context).pushReplacementNamed('/homepage');
      } catch (e) {
        debugPrint("Facebook signin error: " + e.toString());
      }
      break;
  }
  }
  
  bool isLoggedIn = false;
  
  void onLoginStatusChanged(bool isLoggedIn) {
  setState(() {
    this.isLoggedIn = isLoggedIn;
  });
  }
  
  
  }
  }
  </code></pre>
  
  <p><strong>LoginPage.dart</strong> <strong>(NEW)</strong></p>
  
  <pre><code>class _LoginPageState extends State&lt;LoginPage&gt; {
  bool loading = false;
  
  bool isLoggedIn = false;
  
  void onLoginStatusChanged(bool isLoggedIn) {
  setState(() {
    this.isLoggedIn = isLoggedIn;
  });
  }
  
  @override
  void initState() {
  super.initState();
  isSignedIn();
  }
  
  void isSignedIn() async {
  setState(() {
    loading = true;
  });
  
  isLoggedIn = await _googleSignIn.isSignedIn();
  
  if (isLoggedIn) {
    Navigator.pushReplacement(
        context, MaterialPageRoute(builder: (context) =&gt; HomePage()));
  }
  
  setState(() {
    loading = false;
  });
  }
  }
  </code></pre>
  
  <p>I have to add this code in order to stay signed in. By the way, how do i able to make the same for Facebook?</p>