【发布时间】:2021-06-02 09:53:56
【问题描述】:
我正在尝试创建一个社交应用程序,其中最后一个聊天室显示在主页中, 每个 ChatRoom 文档都包含
[lastSentMessage,lastSentMessageBy,LastsentMessageDate,用户数组 一起聊天的人]
所以我现在面临的问题是,当我使用 StreamBuilder 在自定义小部件中显示这些聊天室时,我构建该小部件以单独保存每个聊天室数据(它显示发送消息的用户的姓名,以及他的profile photo and lastSentMessage) StreamBuilder 实际上显示 lastSentMessage 就像魅力一样,但我总是得到相同的显示名称和照片 url 数据,
这里是代码:
String phone;
QuerySnapshot userDataByPhone;
accessUserDataByPhoneNumber(String phoneNumber) async{
// userDataByPhone is a QuerySnapshot to access the document of the user where the phoneNumber matchs the given phoneNumber
userDataByPhone = await FirebaseFirestore.instance.collection("users").where("phonenumber",isEqualTo:phoneNumber ).get();
}
Widget buildMyChatRooms()
{
getChatRoomsStream();
return StreamBuilder(
stream: chatRoomsStream,
builder: (context,snapshot ){
if(snapshot.hasData) {
return ListView.builder (
itemCount:snapshot.data.docs.length,
shrinkWrap: true,
itemBuilder: (context, index) {
DocumentSnapshot doc = snapshot.data.docs[index];
phone = "+"+doc.id.replaceAll(loggedUser.phoneNumber.substring(1), "").replaceAll("_", "");
accessUserDataByPhoneNumber(phone);
return SingleChatRoomWidget(
userDataByPhone.docs[0]["displayname"],
doc["lastSentMessage"],
doc["lastSentMessageBy"],
userDataByPhone.docs[0]["photourl"]) ;
}
);
}
else{
它总是返回相同的数据: enter image description here
这也是我的自定义聊天室代码:
SingleChatRoomWidget(String name, String lastSentMessage,lastSentMessageBy,String profileUrl)
{
return Column(
children: [
SizedBox(height: 15,),
Row(children: [
Container(height: 65,width: 65,decoration: BoxDecoration(shape: BoxShape.circle,image: DecorationImage(image:NetworkImage(profileUrl),fit: BoxFit.cover),),),
SizedBox(width: 8,),
Column(children: [
Text(name,style: TextStyle(color: Colors.deepPurple,fontSize: 16,),),
SizedBox(height: 7,),
lastSentMessageBy==loggedUser.phoneNumber?Text("You: "+lastSentMessage,style: TextStyle(color: Colors.black45,fontSize: 16,),):
Text(lastSentMessage,style: TextStyle(color: Colors.black45,fontSize: 16,),),
],)
],),
Padding(
padding: const EdgeInsets.all(20.0),
child: Container(height: 0.5,width: MediaQuery.of(context).size.height,color: Colors.black45,),
),
],
);
}
【问题讨论】:
标签: firebase flutter firebase-realtime-database