- 我无法从firebase访问uid。我试了很多次。有人能帮我解决这个问题吗?
class Massages extends StatelessWidget {
@override Widget build(BuildContext context) {
return FutureBuilder(
future: Future.value(FirebaseAuth.instance.currentUser),
builder: (ctx, futureSnapshot) {
if (futureSnapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('chat')
.orderBy('createdAt', descending: true)
.snapshots(),
builder: (ctx, AsyncSnapshot<dynamic> chatSnapshot) {
if (chatSnapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
reverse: true,
itemCount: chatSnapshot.data!.docs.length,
itemBuilder: (ctx, index) => MessageBubble(
chatSnapshot.data!.docs[index]['text'],
chatSnapshot.data!.docs[index]['userId'] ==
futureSnapshot.data?.uid,
key: ValueKey(chatSnapshot.data!.documentID),
),
);
});
}); } }
看起来futureSnapshot.data
返回的是Object
而不是User
。您可以尝试使用
((User)futureSnapshot.data)?.uid,