2

I have the following code that I am using to log a user in silently

try {
    result = await GoogleSignIn().signInSilently().catchError((x) {
      print(x);
    });
  } catch (e) {
    print(e);
  }

If the user cannot sign in silently, it results in an error.

PlatformException (PlatformException(sign_in_required, com.google.android.gms.common.api.ApiException: 4: 4: , null))

The problem I am having is that I cannot seem to catch the exception. catchError nor the catch block are being hit. How do I catch this error?

4 Answers 4

2

Do the following in your method

try {
    result = await GoogleSignIn().signInSilently(suppressErrors: false).catchError((x) {
      print(x);
    });
  } catch (e) {
    print(e);
  }

By default suppressErrors = true suppressing the error messages you want to catch.

Looking at the source code

The SignInSilently method is used to suppress error messages thus not throwing the exception you want to catch.

From the docs of this method:

  /// When [suppressErrors] is set to `false` and an error occurred during sign in
  /// returned Future completes with [PlatformException] whose `code` can be
  /// either [kSignInRequiredError] (when there is no authenticated user) or
  /// [kSignInFailedError] (when an unknown error occurred).

Full Method

 /// Attempts to sign in a previously authenticated user without interaction.
  ///
  /// Returned Future resolves to an instance of [GoogleSignInAccount] for a
  /// successful sign in or `null` if there is no previously authenticated user.
  /// Use [signIn] method to trigger interactive sign in process.
  ///
  /// Authentication process is triggered only if there is no currently signed in
  /// user (that is when `currentUser == null`), otherwise this method returns
  /// a Future which resolves to the same user instance.
  ///
  /// Re-authentication can be triggered only after [signOut] or [disconnect].
  ///
  /// When [suppressErrors] is set to `false` and an error occurred during sign in
  /// returned Future completes with [PlatformException] whose `code` can be
  /// either [kSignInRequiredError] (when there is no authenticated user) or
  /// [kSignInFailedError] (when an unknown error occurred).
  Future<GoogleSignInAccount> signInSilently({bool suppressErrors = true}) {
    final Future<GoogleSignInAccount> result = _addMethodCall('signInSilently');
    if (suppressErrors) {
      return result.catchError((dynamic _) => null);
    }
    return result;
  }

Reference

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for your input. The problem is that the error is being thrown and it just doesn't get caught then my function that wraps the signInSilently code returns a _TypeError type and doesn't run the code in the catch block.
1

This might help.

Checklists :

  1. Did not register a sha fingerprint.
  2. Make sure to have my "support email" set.
  3. Enable the Google Sign-in method.

1 Comment

Hi, thanks for your input. The problem is that the catch code is not hit ever.
0
+50

The catchError block is actually getting hit if you use suppressErrors = false
Try the following code and see what it prints on your console.

result = await GoogleSignIn().signInSilently(suppressErrors: false).
  catchError((x) {
    print("inside catchError");
  });

1 Comment

Thank you, this helped me on my way as I deleted the print(e) line!
0

The problem is the print(e); line in the catch statement. 'e' is not a string so an error occurs. I don't know why but no breakpoints are hit in the catch statement for this error and no error is output to the console. If I put a catch statement around a function that calls the login code ie.

void tryLogin(){
  try{
    myLoginLogic();
  } catch(e) {
    print(e);
  }
}

Then I do get an error message

Unhandled Exception: type '_TypeError' is not a subtype of type 'String'

So to be clear the correct code would be

try {
  result = await GoogleSignIn().signInSilently().catchError((e) {
    print(e.toString());
  });
} catch (e) {
  print(e.toString());
}

Why code doesn't break on error and no message is written to the console for the original function I don't know. Also what I don't understand is why, even when the catch statement is written without errors, in debug mode the code breaks on the signInSilently() line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.