如何在 GoogleSignIn signInSilently 中捕获错误?
Posted
技术标签:
【中文标题】如何在 GoogleSignIn signInSilently 中捕获错误?【英文标题】:How to catch errors in GoogleSignIn signInSilently? 【发布时间】:2019-11-08 14:24:03 【问题描述】:我有以下代码用于静默登录用户
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 块都被击中。如何发现此错误?
【问题讨论】:
【参考方案1】:在您的方法中执行以下操作
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;
参考
Google Sign In Plugin Repo Method Source Code Starting Line 283【讨论】:
您好,感谢您的意见。问题是错误被抛出并且它没有被捕获然后我包装signInSilently代码的函数返回一个_TypeError类型并且不运行catch块中的代码。【参考方案2】:This 可能会有所帮助。
清单:
-
未注册 sha 指纹。
确保设置了我的“支持电子邮件”。
启用 Google 登录方法。
【讨论】:
您好,感谢您的意见。问题是捕获代码永远不会被命中。【参考方案3】:如果你使用suppressErrors = false
,catchError
块实际上会被击中
试试下面的代码,看看它在你的控制台上打印了什么。
result = await GoogleSignIn().signInSilently(suppressErrors: false).
catchError((x)
print("inside catchError");
);
【讨论】:
谢谢你,这在我删除 print(e) 行时帮助了我!【参考方案4】:问题是catch 语句中的print(e);
行。 'e' 不是字符串,因此会发生错误。我不知道为什么,但是在 catch 语句中没有针对这个错误命中断点,并且没有错误输出到控制台。如果我在调用登录代码的函数周围放置一条 catch 语句,即。
void tryLogin()
try
myLoginLogic();
catch(e)
print(e);
然后我确实收到一条错误消息
Unhandled Exception: type '_TypeError' is not a subtype of type 'String'
所以要清楚正确的代码是
try
result = await GoogleSignIn().signInSilently().catchError((e)
print(e.toString());
);
catch (e)
print(e.toString());
为什么代码不会因错误而中断,并且我不知道原始函数不会向控制台写入任何消息。另外我不明白的是,为什么即使在没有错误的情况下编写了 catch 语句,在调试模式下代码也会在 signInSilently()
行中断。
【讨论】:
以上是关于如何在 GoogleSignIn signInSilently 中捕获错误?的主要内容,如果未能解决你的问题,请参考以下文章
如何查看 GoogleSignIn 用户上传的图片或谷歌设置的默认图片
如何从 Expo GoogleSignIn API (expo v32) 获取 idToken?
当我使用 googleSignIn 时,用户在 Firestore 上重复
无法解析 GoogleSignIn 和 GoogleSignInClient