我試圖通過從他們的 Fire Store 檔案中拉出用戶的“imageUrl”,在他們的主頁上顯示用戶的個人資料圖片。我已經將應用程式設定為用戶可以上傳更新 Fire Store 中“imageUrl”的新影像,但我不知道如何將“imageUrl”作為變數,以便我可以在應用程式螢屏上顯示它.
我一直在在線閱讀檔案,但它似乎過于簡化或過時。我試過使用StreamBuilder,但它從資料庫中的每個用戶而不是單個用戶中提取資料。我只需要知道如何在我的 dart 代碼中使用“getString()”和檔案參考或我已有的集合參考來提取這個值并將其用作變數,謝謝。

class _UserPageState extends State<UserPage> {
User user = auth.currentUser!;
final CollectionReference collectionReference = FirebaseFirestore.instance.collection('users');
// Get profileImageUrl from users userDoc
String imageUrl = 'test'; // this should be the users imageUrl
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'${user.email}'), // this is being pulled from authentication not firestore
),
body: Center(
child: Column(
children: [
// --------------------------- I tried using a stream builder here ---------------------
StreamBuilder(
stream: collectionReference.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return const Text(
'Something went wrong.'); // A: use incase the data does not load
}
final data = snapshot.requireData;
return ListView.builder(
shrinkWrap: true,
itemCount: data.size,
itemBuilder: (context, index) {
return Text(
// A: Stream builder will update with all of the users email addresses, I want this for one user exclusively
'My email is ${data.docs[index]['email']}');
},
uj5u.com熱心網友回復:
collection('users')
.where("uid", isEqualTo: uid)
.snapshots(),
要過濾 Firestore 集合中的資料,請使用“where”。離線存盤用戶 uid 并通過 where 使用存盤的 uid 查詢
uj5u.com熱心網友回復:
您可以使用以下函式從流中獲取單個資料。
Stream<UserModel> getSingleStreamData({String? uId}) {
return ref!.where(CommonKeys.id, isEqualTo: uId).snapshots().map((value) => value.docs.first.data());}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/372202.html
標籤:扑 谷歌云firestore
