简体   繁体   English

在 Flutter 应用程序中访问登录 Firebase 用户的最简洁方法是什么?

[英]What is the cleanest way to access a logged in Firebase user across your Flutter app?

I am using firebase in my flutter app and accessing the logged in user would be done something like this -我在我的 flutter 应用程序中使用了 firebase,访问登录用户的方式如下 -

  Future<FirebaseUser> getLoggedInUser() async {
    return await FirebaseAuth.instance.currentUser();
  }

I use BLOC and was thinking of adding this to my AuthenticationBloc class.我使用 BLOC 并考虑将其添加到我的AuthenticationBloc类中。 Since this BLOC is injected early, I should be able to access this from anywhere in the app -由于这个 BLOC 是早期注入的,我应该能够从应用程序的任何地方访问它 -

AuthenticationBloc authBloc = BlocProvider.of<AuthenticationBloc>(context);
FirebaseUser user = await authBloc.getLoggedInUser()

But this leads to a few problems like accessing this in a StreamBuilder becomes an issue because of the need of using await which means I would need to make the StreamBuilder async (not sure how).但这会导致一些问题,例如在 StreamBuilder 中访问它会成为一个问题,因为需要使用 await 这意味着我需要使 StreamBuilder 异步(不确定如何)。

What would be the best/cleanest way of accessing the user object FirebaseUser user from anywhere in the app ?从应用程序的任何位置访问用户对象FirebaseUser user的最佳/最干净的方法是什么?

Thanks !谢谢 !

Edit 1 :编辑1:

So one way to do this would be to use a stateful widget every time I want the current user因此,一种方法是在每次需要当前用户时使用有状态小部件

class _MyWidgetState extends State<MyWidget> {
  FirebaseUser user;

  @override
  void initState() {
    super.initState();
    _updateCurrentUser();
  }

  void _updateCurrentUser() async {
    FirebaseUser currUser = await Firebase.instance.currentUser();
    setState(() {
      user = currUser;
    });
  }

But using a stateful widget every time I want the currUser seems weird.但是每次我想要 currUser 时都使用有状态的小部件似乎很奇怪。 Is there a better way out ?有没有更好的出路?

You can access the user by using stream such as您可以使用流访问用户,例如

StreamBuilder(
  stream:FirebaseAuth.instance.onAuthStateChanged,
  builder:(context,snapshot){
     if (snapshot.connectionState == ConnectionState.active) {
        final user = snapshot.data;
         return Text("User Id:${user.uid}");
        }else{
         return Center(
            child:CircularProgressIndicator(),
         );
      }
  }

);

暂无
暂无

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

相关问题 如何使用 flutter 中的 web 应用程序检查用户是否使用 firebase 登录? - How to check user is logged in or not with firebase for web App in flutter? Firebase Flutter应用程序是否可以在脱机应用程序重新启动后保留数据? - Is there a way for Firebase Flutter apps to persist data across offline app restarts? 跨Ionic标签管理Firebase用户的最佳方法是什么 - What is the best way to manage Firebase user across Ionic Tabs 使用 Flutter 图标将 Firebase 添加到您的 Flutter 应用程序 - Adding Firebase to your Flutter app with Flutter Icon 检查用户是否登录 Flutter &amp; firebase auth | - Check if the user is logged in Flutter & firebase auth | 我是 flutter web 的新手,如何使用 firebase 电话身份验证对用户进行身份验证,有没有办法让用户保持登录状态? - I am new to flutter web, how to authenticate users using firebase phone authentication, is there a way to keep user logged in? 在 Firebase Flutter 中,如何使用自定义类访问登录用户的详细信息 - In Firebase Flutter how do I access details of the logged in user using my custom class Flutter app根据用户所在位置访问不同的firebase项目 - Flutter app access different firebase project based on the location of the user 从 flutter 应用程序发送 FCM 时得到 403,登录用户不是 firebase 帐户所有者 - Got 403 while sending FCM from flutter app with logged in user other then firebase account owner 检查用户是否是firebase auth中的新用户的最佳方法是什么? - What is the best way to check if the user is new in firebase auth with flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM