gpt4 book ai didi

flutter - 如何捕获 GoogleSignIn signInSilently 中的错误?

转载 作者:IT王子 更新时间:2023-10-29 06:50:32 31 4
gpt4 key购买 nike

我有以下代码用于静默登录用户

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

如果用户无法静默登录,则会导致错误。

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

我遇到的问题是我似乎无法捕捉到异常。 catchError 和 catch block 都被命中。我如何捕获此错误?

最佳答案

在您的方法中执行以下操作

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

默认情况下,suppressErrors = true 抑制您想要捕获的错误消息。

查看 source code

SignInSilently 方法用于抑制错误消息,从而不会抛出您想要捕获的异常。

来自该方法的文档:

  /// 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).

完整方法

 /// 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;
}

引用

关于flutter - 如何捕获 GoogleSignIn signInSilently 中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56776203/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com