简体   繁体   English

如何保持用户登录 Flutter Firebase 应用程序?

[英]How to keep user signed in Flutter Firebase app?

I am creating a chat app that uses Firebase for the backend.我正在创建一个使用 Firebase 作为后端的聊天应用程序。 Whenever I restart the app, it goes to the welcome screen where users have to login/register, every single time.每当我重新启动应用程序时,它都会进入欢迎屏幕,用户必须每次登录/注册。 If you log in, then close the app without logging out, it still goes back to the login screen.如果您登录,然后在不注销的情况下关闭应用程序,它仍会返回登录屏幕。 I have added a method in the main method to check:我在 main 方法中添加了一个方法来检查:

void getCurrentUser() async {
FirebaseAuth.instance.onAuthStateChanged.listen((firebaseUser) {
  print(firebaseUser);
  _user = firebaseUser;
});
}

So, if _user is null, then go to welcome page, otherwise go straight into the chat screen of the app:因此,如果 _user 是 null,则 go 到欢迎页面,否则 go 直接进入应用程序的聊天屏幕:

Widget build(BuildContext context) {
getCurrentUser();
return MaterialApp(
  initialRoute: _user == null ? '/' : '/chat',
  routes: {
    '/': (context) => WelcomeScreen(),
    '/login': (context) => LoginScreen(),
    '/registration': (context) => RegistrationScreen(),
    '/chat': (context) => ChatScreen(),
  },
);
}

Why doesn't it work?为什么它不起作用? I have Googled this problem for a while and earlier I was using auth.currentUser(), which could have been the problem, but now I'm using a listener and it still doesn't work.我已经用谷歌搜索了这个问题一段时间,之前我使用的是 auth.currentUser(),这可能是问题所在,但现在我使用了一个监听器,但它仍然不起作用。

var user await FirebaseAuth.instance.currentUser();
if (user != null) {
    Navigator.of(context).pushNamed("routeName");
}

Wrap the above code in an async function that takes BuildContext and call in the build method before the return statement.将上面的代码包装在一个异步 function 中,它接受 BuildContext 并在 return 语句之前调用 build 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在使用电话号码登录 firebase android 中的用户,如何让用户保持登录状态直到用户注销? - I'm using phone number to to sign in user in firebase android ,how can i keep user signed in until user sign out? 用户在 flutter firebase 中始终处于登录状态且无法注销 - User is always signed in and can't logout in flutter firebase 如何检查登录 Firebase 用户是否有密码 - How to check if signed in Firebase user has a Password 我如何在我的 flutter 应用程序中使用第二个 firebase 项目? - How can i user second firebase project in my flutter app? 使用 firebase google 登录后,如何在 Flutter 应用程序中保存或保留要在我的设置页面中使用的数据 - How to save or keep data to be used in my settings page after sign in using firebase google sign in, in flutter app Firebase:如何让 Android 用户保持登录状态? - Firebase: How to keep an Android user logged in? Firebase 侦听器未侦听已登录用户 - Firebase listener not listening to signed in user 如何确定 Firebase 用户是否使用 Facebook 身份验证登录 - How to determine if a Firebase user is signed in using facebook authentication firebase如何检测到用户已在android上登录? - How does firebase detect that a user is already signed in on android? 如何检测用户是否已使用Firebase Auth匿名登录? - How can i detect if user is signed in anonumously with firebase auth?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM