本文介绍了如何在Flutter中使用.currentUser方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码:

  getFavSalons(AsyncSnapshot<QuerySnapshot> snapshot) {
return snapshot.data.documents
    .map((doc) => SalonBlock(
          salonName: doc["salonName"],
          location: doc["location"],
          workTime: doc["workTime"],
          rating: doc["rating"],
        ))
    .toList();

}

以及构建列表的部分代码:

and part of code where I building list:

             StreamBuilder(
                  stream: Firestore.instance
                      .collection("customers")
                      .document("HAQaVqCPRfM7h6yf2liZlLlzuLu2")
                      .collection("favSalons")
                      .snapshots(),
                  builder:
                      (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                    if (snapshot.hasData) {
                      return Container(
                        margin:
                            EdgeInsets.only(bottom: screenHeight * 0.33),
                        child: new ListView(
                          children: getFavSalons(snapshot),
                        ),
                      );
                    }
                    return LoadingSalon();
                  }),

在这里我使用uid:

.document("HAQaVqCPRfM7h6yf2liZlLlzuLu2")

在这里,我必须使用currentUser来代替自己.该怎么做?

here I have to use currentUser instead of filling myself. How to do this?

推荐答案

应用程序中的当前用户可以随时更改.例如:

The current user in you application can change at any moment. For example:

  • 当用户启动应用程序时,Firebase会自动恢复其先前的身份验证状态.但这要求它调出服务器,因此用户在登录之前暂时不会登录( currentUser null ).
  • 用户登录时,Firebase每小时都会刷新其身份验证状态,以确保其登录仍然有效(例如,尚未禁用其帐户).这意味着即使您未显式调用API,其登录状态也可以更改.

由于这些原因,您不能简单地调用 currentUser 并期望它保持有效.相反,您应该附加身份验证状态更改侦听器,这将为您提供一个身份验证状态流.

For these reasons you can't simply call currentUser and expect it to remain valid. Instead you should attach an auth state change listener, which gives you a stream of authentication states.

在构建UI的代码中,您可以在另一个流构建器中使用此用户数据流.因此,您将拥有两个嵌套的流构建器:

In your code that builds the UI, you can use this stream of user data inside another stream builder. So you'll have two nested stream builders:

  1. 用于用户身份验证状态.
  2. 对于数据库,基于当前用户.

类似(到目前为止尚未试用):

So something like (untested for now):

 StreamBuilder(
     stream: FirebaseAuth.instance.authStateChanges(),
     builder: (context, AsyncSnapshot<User> snapshot) {
        if (snapshot.hasData) {
          return StreamBuilder(
              stream: Firestore.instance
                  .collection("customers")
                  .document(snapshot.data.uid)
                  .collection("favSalons")
                  .snapshots(),
              builder:
                  (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasData) {
                  return Container(
                    margin:
                        EdgeInsets.only(bottom: screenHeight * 0.33),
                    child: new ListView(
                      children: getFavSalons(snapshot),
                    ),
                  );
                }
                return LoadingSalon();
              }),
        }
        return Text("Loading user...");
      }),
         

这篇关于如何在Flutter中使用.currentUser方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 02:49