Home > Software design >  Not selecting an account in google account login in flutter bug on still entering the home page of t
Not selecting an account in google account login in flutter bug on still entering the home page of t

Time:06-23

So implementing this google account sign in feature for the app that I'm building and i encounter this bug on which if i dont select an account like click outside the pop up selection of google account or press the back button i can still enter the home page of the app, i dont know how to catch this error please help me, this is my code

 ElevatedButton.icon(
                                style: ElevatedButton.styleFrom(
                                  primary: Colors.white,
                                  onPrimary: Colors.black,
                                  minimumSize: const Size(double.infinity,50),
                                ),
                                icon: const FaIcon(FontAwesomeIcons.google, color: Colors.red,),
                                label: const Text("Login using Gmail"),
                                onPressed: () async{
                                  
                                  try {await FirebaseServices().signInWithGoogle().catchError((onError)=>print(onError));
                                  if (FirebaseServices().signInWithGoogle() == null) return null;
                                  Navigator.push(context,
                                  MaterialPageRoute(builder: (context)=> const HomePage()));
                                  }
                                  on FirebaseAuthException catch (e){
                                    print(e.message);
                                    throw e;
                                    }
                                },
                              ),


here's a video about the bug enter image description here

CodePudding user response:

The logic in your "onPressed" login button should only authenticate, but not push the homePage.

The recommended solution is to have a "landingPage" listening to FirebaseAuth.instance.authStateChanges() and checking the user. If it is null, push AuthenticationPage, otherwise homePage.

You can find more details below:

https://firebase.google.com/docs/auth/flutter/start

https://pub.dev/packages/google_sign_in/example (this example listens to a different stream) but has the same logic

CodePudding user response:

When you are first calling the FirebaseServices.signInWithGoogle().catchError(); You are awaiting an error result. I would suggest you that you call it in this way:

try {
    // Here, we use the .. (cascade) operator, which will give us the
    // reference to the correct future that signInWithGoogle() returns.
    var result = await FirebaseServices().signInWithGoogle()..catchError((onError) => print(onError.toString()));

    // If we didn't sign in, then do not proceed to the home screen
    if (result == null) return;

    // We push a replacement route so that when the user 
    // consecutively presses the back button, he doesn't reach the
    // login screen before exiting the app.
    Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=> const HomePage()));
} on FirebaseAuthException catch(e) {
    print(e.message);
    // Do not throw an exception in your production app. It will make the app crash. Instead, 
    // display it to the user with a Dialog.
    throw e;
}

If my answer was helpful, please mark my answer as Correct. Thank you!

Also, I recommend you to follow @Saichi Okuma's answer, as that is the correct approach to signing-in the user in a production app.

  • Related