Flutter:即使使用 Firebase Auth 关闭应用程序,如何保持用户登录状态

问题描述 投票:0回答:1

我在 Flutter 应用程序中使用 Firebase Auth。用户使用“Auth”进行身份验证并登录,但当应用程序关闭/从移动内存中清除时,用户会自动注销。

即使应用程序关闭,如何保持用户登录并使其状态/会话处于活动状态。

探索了类似的问题,但没有找到满意的答案。

如有任何帮助,我们将不胜感激。谢谢!

flutter session firebase-authentication sharedpreferences
1个回答
0
投票

FirebaseAuth 有一个名为 authStateChanges 的流,它检查用户是否存在。我们可以在 Streambuilder 中使用这个流:

return MaterialApp(
    home: StreamBuilder(
      stream: FirebaseAuth.instance.authStateChanges(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return HomePage();
        }
        if (snapshot.hasError) {
          return Scaffold(body: Text(snapshot.error.toString()));
        }
        return SignInPage();
      },
    ),
  );
© www.soinside.com 2019 - 2024. All rights reserved.