【问题标题】:function SignOut Firebase returns true - Flutter函数 SignOut Firebase 返回 true - Flutter
【发布时间】:2019-12-03 19:33:33
【问题描述】:

当我在应用程序中按下“注销”时,currentUser 变为 null 但并没有真正注销应用程序,我正在使用 ScopedModel,如果我 print(model.isLoggedIn()) 返回 true,则表示存在某个用户在应用中,但用户为空。

当我按下退出时,我在输出时收到一条消息:

D/FirebaseAuth( 5252): Notifying id token listeners about a sign-out event.
D/FirebaseAuth( 5252): Notifying auth state listeners about a sign-out event.
D/FirebaseAuth( 5252): Notifying id token listeners about a sign-out event.
D/FirebaseAuth( 5252): Notifying auth state listeners about a sign-out event.
I/flutter ( 5252): true ( print in model.isLoggedIn() )

遵循退出代码

  void signOut() async{
    print(isLoggedIn());
    await FirebaseAuth.instance.signOut();
    userData = Map();
    notifyListeners();
    print(isLoggedIn());
  }

  bool isLoggedIn(){ return FirebaseUser != null; }

这是关于 UserModel 的完整代码: https://github.com/willsgobi/coleirapet/blob/master/lib/models/loginmodel.dart

【问题讨论】:

  • "currentUser 变为 null 但并没有真正退出应用程序" 你的意思是他们没有真正退出应用程序? isLoggedIn 是做什么的?
  • “currentUser 变为 null 但并没有真正注销应用程序”:当我按下 Logout 时,currentUser 应该为 false,isLoggedIn 应该返回 false,但 isLoggedIn 之后返回 true,然后我调用函数 FirebaseUser .signOut(),表示auth有一些currentUser
  • 函数 isLoggedIn 验证 FirebaseUser 是真还是假:``` bool isLoggedIn(){ return FirebaseUser != null; } ```
  • 在下面回答,但请在以后的问题中确保minimal, complete/standalone code that is needed to reproduce the problem is present in your question itself。我建议研究该链接,因为它是提高获得代码相关问题帮助机会的最佳方式。

标签: firebase flutter firebase-authentication flutter-scoped-model


【解决方案1】:

FirebaseUser 变量是类的一个字段,您可以在用户登录时自行设置。例如,在您的代码库中的代码中是这样的:

_auth.createUserWithEmailAndPassword(
    email: userData["email"],
    password: pass
).then((user) async {
      firebaseUser = user.user;

当用户退出时,您不会更新此 firebaseUser 字段,因此它仍指向前一个对象。您需要强制重新加载,如下所示:

await FirebaseAuth.instance.signOut();
firebaseUser = await await _auth.currentUser();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-23
    • 2020-01-22
    • 2021-03-10
    • 2017-12-15
    • 2021-06-11
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多