我想從 Firestore 檢索資料并在我的 flutter 應用程式中顯示它們,但該檔案有一個名為“用戶”的地圖,我的問題是如何訪問此地圖欄位?

StreamBuilder(
stream: FirebaseFirestore.instance
.collection('Channels')
.where('participants', arrayContains: UserInfoData.userID)
.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
switch (streamSnapshot.connectionState) {
case ConnectionState.waiting:
return Center(
child: CircularProgressIndicator(),
);
default:
if (streamSnapshot.hasError) {
print(streamSnapshot.hasError.toString());
} else {
final channels = streamSnapshot.data!.docs;
if (channels.isEmpty) {
return Text('ff');
} else
return ListView.builder(
itemCount: channels.length,
itemBuilder: (context, i) => ListTile(
leading: CircleAvatar(
backgroundImage:
NetworkImage(channels[i]['announceImage']),
),
title: Text(channels[i]['reciverID']),
),
);
}
}
return Text('ok');
},
),
);
uj5u.com熱心網友回復:
您可以在此處獲取值,就像任何其他包含鍵的地圖一樣。在您的情況下,users是一個包含鍵lastname, name, userid的映射。channels[i]['users']會為您提供用戶地圖。要獲取特定鍵的值,您可以將channels[i]['users']視為一個整體,就像一個陣列。
因此,您可以通過執行以下操作來獲取值:
String lastname = channels[i]['users']['lastname']
String name = channels[i]['users']['name']
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/329001.html
上一篇:組合多個集合組查詢
