0
votes

For user logged in

 Future<String> signInWithGoogle() async {
        final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
        final GoogleSignInAuthentication googleSignInAuthentication =
        await googleSignInAccount.authentication;
    
        final AuthCredential credential = GoogleAuthProvider.getCredential(
          accessToken: googleSignInAuthentication.accessToken,
          idToken: googleSignInAuthentication.idToken,
        );
    
        final AuthResult authResult = await _auth.signInWithCredential(credential);
    
        final FirebaseUser user = authResult.user;
        assert(!user.isAnonymous);
        assert(await user.getIdToken() != null);
        final FirebaseUser currentUser = await _auth.currentUser();
        assert(user.uid == currentUser.uid);
        if(authResult.additionalUserInfo.isNewUser == true){
          InsertNewUserDetails(user.displayName,user.email,user.photoUrl,user.phoneNumber);
        }
        if(authResult.user != null){
          email = user.email;
          name = user.displayName;
          imageUrl =user.photoUrl;
          globalInstance.isLoggedIn = true;
          print(globalInstance.isLoggedIn);
    
        }
        return 'signInWithGoogle succeeded: $user';
      }

This first time sign in Loginpage checking for is logged account

class MyApp extends StatelessWidget {
  @override
  String strinbggg;
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.teal,
        body: StreamBuilder<FirebaseUser>(
          stream: FirebaseAuth.instance.onAuthStateChanged,
          builder: (BuildContext context, snapshot) {
            if (snapshot.hasData && (!snapshot.data.isAnonymous)) {
//             return Text('${snapshot.hasData}');
              return Homepage();
            } else {
//              return SafeArea(
//                maintainBottomViewPadding: true,
//                  child: Text('aasdasdfasfdh : ${snapshot.data.email}',style: TextStyle(color: Colors.black54),));
              return LoginPage();
            }
          },
        ),
      ),
    );
  }
}

When first time login with google its working. When i was close application and start again it will redirecting to the homepage fine but user profile details like displayName , email are returning null value

signInwithgoogle() is use for the first time login that store the user details in firestore and user details set from here

MyApp is starting point of application that will check is user is already logged in

1
can you please show the code in which you are checking for login state when you start the application ? - Vilsad P P
body: StreamBuilder<FirebaseUser>( stream: FirebaseAuth.instance.onAuthStateChanged, builder: (BuildContext context, snapshot) { if (snapshot.hasData && (!snapshot.data.isAnonymous)) { // return Text('${snapshot.hasData}'); return Homepage(); } else { // return SafeArea( // maintainBottomViewPadding: true, // child: Text('aasdasdfasfdh : ${snapshot.data.email}',style: TextStyle(color: Colors.black54),)); return LoginPage(); } - Muzammil
that is not the code for google signin, it is a stream which is used to handle the signing state, like, user signed in, user signed out etc. and from your code, i can assume that you are handling it to show login / home page. you need to check the user is allready signed or not. - Vilsad P P

1 Answers

0
votes

I would suggest, you check the user is already signed in or not using the method

bool isSignedIn = await _googleSignIn.isSignedIn();

and if the user is signed in, then get the credentials using

user = await _auth.currentUser();

when you start the app. In case you are not logged in, show login page. Any example would be something like below

final auth = FirebaseAuth.instance;

class MyApp extends StatelessWidget {
  @override
  String strinbggg;
  Widget build(BuildContext context) {

      return MaterialApp(
      home:  (_isSignedIn() == true ? new Homepage() : new LoginPage()));
   }
   bool _isSignedIn() async {
      bool isSignedIn = await _googleSignIn.isSignedIn();
      if(isSignedIn){
          GoogleSignInAccount user = await auth.currentUser;
          //You can store this in your state.
     }
      return !(user == null && auth.currentUser == null);
   }
}

Please note this is a in editor composed code, not tested, you will have to make adjustments.