【问题标题】:Flutter List UI not updating properlyFlutter List UI 未正确更新
【发布时间】:2020-12-04 01:59:52
【问题描述】:

我正在使用 Flutter 和 firebase 构建一个一对一的聊天应用程序,我想在它发生的那一天的标签下显示所有聊天,就像在所有主要的聊天应用程序上一样。

我按照NiklasLehnfeld 的建议,在每条消息的时间戳字段上使用 order by 命令,按时间升序从 firestore 检索数据 我使用集合包中的 groupBy 方法对我的消息进行分组,并使用嵌套列表视图构建器,外部用于创建组,内部用于向这些组填充数据。

这里是代码

  Widget build(BuildContext context) {
    return FutureBuilder(
      future: FirebaseAuth.instance.currentUser(),
      builder: (ctx, futureSnapshot) {
        if (futureSnapshot.connectionState == ConnectionState.waiting) return Center(child: CupertinoActivityIndicator());
        return StreamBuilder(
          stream: Firestore.instance
              .collection('mychats/${futureSnapshot.data.uid}/${widget.recieverUid}')
              .orderBy('createdAt', descending: true)
              .snapshots(),
          builder: (context, chatSnapshots) {
            if (chatSnapshots.connectionState == ConnectionState.waiting)
              return Center(child: CupertinoActivityIndicator());
            else {
              final List chatDocs = chatSnapshots.data.documents;

              Map<String, List> grouped = groupBy(chatDocs, (chat) {
                String dateTime = chat['dateTime'];
                return dateTime;
              });
              (grouped.forEach((m, v) {
                print('$m');
                for (int i = 0; i < v.length; i++) {
                  print(v[i]['text']);
                }
              }));
              return ListView.builder(
                  reverse: true,
                  itemCount: grouped.keys.length,
                  itemBuilder: (ctx, index1) {
                    String date = grouped.keys.toList()[index1];
                    List messages = grouped[date];
                    return Column(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Text(date),
                        ListView.builder(
                            reverse: true,
                            primary: false,
                            shrinkWrap: true,
                            itemCount: messages.length,
                            itemBuilder: (context, index) {
                              return MyMessageBubble(
                                  chatDocs[index].documentID,
                                  chatDocs[index]['text'],
                                  (chatDocs[index]['userId'] == futureSnapshot.data.uid) ? true : false,
                                  ValueKey(chatDocs[index].documentID));
                            })
                      ],
                    );
                  });
            }
          },
        );
      },
    );
  }

这是我正在获取的消息列表

这是 UI。我在控制台中打印的结果在 UI 中没有正确显示。我尝试为两个列表构建器设置键,但没有成功。有什么办法可以纠正

【问题讨论】:

  • 问题是什么?
  • 您能否详细说明您面临的问题?
  • 我已经编辑了这个问题。该列表未显示我在控制台上打印的数据

标签: flutter dart flutter-layout


【解决方案1】:

您使用了错误的列表:

这个:

chatDocs[index].documentID,
chatDocs[index]['text'],
(chatDocs[index]['userId'] == futureSnapshot.data.uid) ? true : false,
ValueKey(chatDocs[index].documentID));

应该引用messages,而不是chatDocs。因为indexmessages 的索引。

【讨论】:

    猜你喜欢
    • 2021-03-21
    • 2020-12-17
    • 2022-12-30
    • 2023-03-17
    • 2022-01-14
    • 2021-12-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多