我有一些代码:
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来代替自己。这该怎么做?
最佳答案
应用程序中的当前用户可以随时更改。例如:
currentUser
是null
)。由于这些原因,您不能简单地调用
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/