【问题标题】:Log out button for different type of accounts in flutter firebase AuthFlutter Firebase Auth中不同类型帐户的注销按钮
【发布时间】:2022-07-07 19:13:47
【问题描述】:

我有一个带有多个登录选项(电子邮件和密码、Google 帐户、电话号码)的 Flutter 应用。一旦用户登录到应用程序,他就会有一个抽屉。 这个抽屉里有一个退出按钮,当用户按下它时应该退出并将他送回注册页面。所以我一直在使用 firebase auth 的注销实例 await FirebaseAuth.instance.signOut();

但是自从我添加了 google 登录选项后,我开始面临一个问题,即对于 google 退出,我需要先断开用户的连接 await googleSignIn.disconnect(); 所以我在退出函数中添加了这一行,如下所示:

  Future<void> SignOut() async {
 await googleSignIn.disconnect();
 await FirebaseAuth.instance.signOut();
 Navigator.pushAndRemoveUntil(
   context,
   MaterialPageRoute(
     builder: (BuildContext context) => AuthPage(),
   ),
       (route) => false,
 );}

当用户使用 google 登录时,此按钮可以正常工作,但如果他使用其他方法登录,则会引发错误:

未处理的异常:PlatformException(status, Failed to disconnect., null, null)

如何处理不同类型的用户一键退出?

【问题讨论】:

  • 检查google登录是否为空,如果不为空则仅退出
  • @AmanpreetKaur 在这种情况下,如果用户使用电话号码登录,那么谷歌登录将始终为空,他将永远无法退出
  • 我实际上是通过在退出按钮上添加条件来解决的。 Future SignOut() async { if (googleSignIn.currentUser != null){ await googleSignIn.disconnect();等待 FirebaseAuth.instance.signOut();} else{ 等待 FirebaseAuth.instance.signOut(); } 谢谢

标签: flutter android-studio dart firebase-authentication google-signin


【解决方案1】:
Future<void> SignOut() async {
if (googleSignIn.currentUser != null){
  await googleSignIn.disconnect();
  await FirebaseAuth.instance.signOut();}
else{
  await FirebaseAuth.instance.signOut();}

【讨论】:

    猜你喜欢
    • 2020-08-09
    • 1970-01-01
    • 2018-11-30
    • 2020-10-05
    • 2019-08-12
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    相关资源
    最近更新 更多