【问题标题】:get another document data using StreamBuilder with flutter and Firestore使用带有 Flutter 和 Firestore 的 StreamBuilder 获取另一个文档数据
【发布时间】: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


    【解决方案1】:

    只需在此处将0 替换为index

    SingleChatRoomWidget(
                  userDataByPhone.docs[0]["displayname"],
                  doc["lastSentMessage"],
                  doc["lastSentMessageBy"],
                  userDataByPhone.docs[0]["photourl"])
    

    变成这样:

    SingleChatRoomWidget(
                  userDataByPhone.docs[index]["displayname"],
                  doc["lastSentMessage"],
                  doc["lastSentMessageBy"],
                  userDataByPhone.docs[index]["photourl"])
    

    【讨论】:

    • 我已经尝试过了,它给了我: RangeError (index): Invalid value: Only valid value is 0: 1
    • 检查是否有超过 1 个用户存储在数据库中,如果没有问题,那么很明显 userDataByPhone 是问题所在,所以必须看看它是如何设置的,不要忘记如果有数据准备好,它必须查找。
    猜你喜欢
    • 2023-01-01
    • 2020-03-04
    • 2019-10-28
    • 2020-02-16
    • 2020-10-01
    • 2020-07-30
    • 2020-02-14
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多