firebase - 如何在Flutter中使用.currentUser方法

标签 firebase flutter dart google-cloud-firestore firebase-authentication

我有一些代码:

  getFavSalons(AsyncSnapshot<QuerySnapshot> snapshot) {
return snapshot.data.documents
    .map((doc) => SalonBlock(
          salonName: doc["salonName"],
          location: doc["location"],
          workTime: doc["workTime"],
          rating: doc["rating"],
        ))
    .toList();
}
以及我建立 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来代替自己。这该怎么做?

最佳答案

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

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

  • 由于这些原因,您不能简单地调用currentUser并期望它保持有效。相反,您应该使用attach an auth state change listener,它为您提供了身份验证状态流。
    在构建UI的代码中,您可以在另一个流构建器中使用此用户数据流。因此,您将拥有两个嵌套的流构建器:
  • 用于用户身份验证状态。
  • 对于数据库,基于当前用户。

  • 所以像(到目前为止尚未试用):
     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...");
          }),
             
    

    关于firebase - 如何在Flutter中使用.currentUser方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63632366/

    相关文章:

    dart - Dart将其作为构造函数中的参数传递

    ios - 在 Cocos2dx iOS 项目中添加 Google Firebase

    Firebase 最大项目和应用程序

    flutter - 删除文本和DropdownButton的预告片图标之间的空白区域

    firebase - Firestore 中的 getDocuments() 和 snapshots() 有什么区别?

    flutter - 无法使用外部小部件设置状态

    swift - 如何使用多个 View Controller 使用 Firebase 身份验证?

    ios - 无效 `Podfile` 文件 : Operation not supported on socket error

    flutter - 如何向底部应用栏添加多行