Flutter Google Sign In Error Not Being Caught

Viewed 535

I am using google sign in and firebase to integrate authentication to my app. However I am facing the problem that, when I cancel the sign in (in the sense that I dismiss the popup), I get a

PlatformException (PlatformException(sign_in_canceled, com.google.GIDSignIn, The user canceled the sign-in flow.))

To my knowledge this is supposed to occur, though I am facing the problem that I am not able to catch the error. Here are a few ways in which I have tried to catch this error:

Future loginWithGoogle() async {
    final GoogleSignIn googleSignIn = GoogleSignIn();
    var account;

    try {
      account = await googleSignIn.signIn();
    } on PlatformException catch (e) {
      print('\n\n\n\n\n AN ERROR OCCURED \n\n\n\n\n');
      print(e.message);
    }

    final AuthResult result = await _auth
        .signInWithCredential(GoogleAuthProvider.getCredential(
            idToken: (await account.authentication).idToken, accessToken: (await account.authentication).accessToken));
    setState(() {
      _firestore.collection('Users').document('${result.user.uid}').setData({
        'Profile Picture': result.user.photoUrl,
        'Name': result.user.displayName,
        'Email ID': result.user.email,
        'Phone Number': result.user.phoneNumber,
      });
      isAuthenticating = true;
    });
    if (result.user.phoneNumber == null) {
      Navigator.pushReplacement(
        context,
        MaterialPageRoute(
          builder: (context) => PhoneRegistrationScreen(
            result: result,
          ),
        ),
      );
    }
  }

It doesn't print anything when the error occurs. I have also tried to catch the error with the catchError() callback :

account = await googleSignIn.signIn().catchError((error) {
      print('AN ERROR OCCURED');
    });

Again, it doesn't print anything when the error occurs.

Thanks a lot guys, and I really appreciate your help!

1 Answers

I have fixed this error and, I have found out that the .catchError() callback wasn't being called in the debug mode (which is when you click the Run->Start Debugging button in VSCode).

However, when you type in flutter run -d , then, the .catchError() method gets called back as it is not in debug mode.

To get your preferred simulator's code paste this line of code in the terminal:

instruments -s devices

If that doesn't work, you can also try pasting this:

xcrun simctl list

The .catchError() method will get called unlike before and the code inside that will get executed as expected!

Additionally, the app won't crash anymore with a PlatformException() and instead you will get a log like this one:

[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'uid' was called on null.
Receiver: null

I have been facing this problem in Google Sign In too, in which the .catchError() was not being called!

In conclusion, if you have some error with handling errors in Firebase Authentication, you should first try to first run in through the terminal. Thanks, and I hope this helps!

Related