我目前正在处理我的应用程序的消息部分.我将streambuilder和streams与GroupedListView结合使用,以便可以根据日期对消息进行分组.

然而,我遇到了一些问题,因为GroupedListView将列表作为其"elements"参数.我们知道流不一定是列表.我已经研究过将流转换为列表,但似乎找不到解决方案.

代码如下所示:

Expanded( //so that we can move the text field to the bottom
    child: StreamBuilder(
      stream: db.chatStream(widget.receiverUser.uid),
      builder: (context, snapshot) {
        return GroupedListView<Chat, DateTime>(
          reverse: true, //so that the texts start from bottom to top
          order: GroupedListOrder.DESC, //get the proper order of sent messages
          padding: const EdgeInsets.all(8),
          elements: db.chatStream(widget.receiverUser.uid), //THIS IS WHERE THE PROBLEM IS!!!
          groupBy: (chat) => DateTime(
            chat.dateTime.year,
            chat.dateTime.month,
            chat.dateTime.day
          ),
          groupHeaderBuilder: (Chat chat) => SizedBox(
            height: 40,
            child: Center(
              child: Card(
                color: Colors.black45,
                child: Padding(
                  padding: const EdgeInsets.all(8),
                  child: Text(
                    DateFormat.yMMMd().format(chat.dateTime),
                    style: const TextStyle(color: Colors.white, fontSize: 12),
                  ),
                ),
              ),
            ),
          ),
          itemBuilder: (context, Chat chat) {
            bool isMe = chat.senderId == uid;
            return Align(
              alignment: isMe ? Alignment.centerRight
                  : Alignment.centerLeft,
              child: Column(
                children: [
                  Align(
                    alignment: isMe ? Alignment.centerRight
                        : Alignment.centerLeft,
                    child: Card(
                      color: isMe
                          ? Colors.purpleAccent
                          : Colors.white,
                      elevation: 2,
                      child: Padding(
                        padding: const EdgeInsets.all(12),
                        child: Text(
                          chat.message,
                          style: TextStyle(
                              color: isMe
                                  ? Colors.white
                                  : Colors.black
                          ),
                        ),
                      ),
                    ),
                  ),
                  Align(
                      alignment: isMe
                          ? Alignment.topRight
                          : Alignment.topLeft,
                      child: Padding(
                        padding: isMe ? const EdgeInsets.only(
                            right: 12)
                            : const EdgeInsets.only(left: 12),
                        child: MidText(text: DateFormat('kk:mm').format(
                            chat.dateTime)),
                      )
                  )
                ],
              ),
            );
          }
        );
      }
    ),
  ),

是否有方法将流转换为列表,以便我可以将其传递给"elements"参数?还是我需要采取不同的方法?

我将非常感谢任何帮助!

推荐答案

我不确定你所要求的是否存在.但我要做的是创建一个Stream<List<_YourType_>>,在给出快照的情况下,我会使用data作为我的列表.

PS:如果你像StreamBuilder<List<_YourType_>>(...一样初始化你的StreamBuilder,那么你的快照将是AsyncSnapshot<List<_YourType_>>,它的数据值将已经是List<_YourType_>,不需要强制转换或任何东西!

PS2:如果我是你,我会寻找time package,因为它有.dateDateTime,甚至创建你自己的喜欢:

extension DateTimeExtension on DateTime {
  DateTime get date => isUtc ? DateTime.utc(year, month, day) : DateTime(year, month, day);
}

只是为了让你在没有时间的情况下更容易得到约会.

Flutter相关问答推荐

手势捕获的异常:类型';Double';不是类型转换中类型';Double';的子类型

无法将Flutter 连接到Firebase

如何在默认情况下隐藏appBar并在向下滚动时显示它- Flutter

blocTest错误:没有从`when()`中调用方法存根

在框中保存对象列表时,类型推断无法正常工作

在 flutter 吧蜂巢中拯救主题

带有附加图像的ListTile

带按钮的 Riverpod future Provider

如何从flutter连接PostgreSQL数据库?

在 Flutter 中更改喜欢按钮的 colored颜色

splash_screen 没有被推送到登录页面

在向 Google Route API 发送 HTTP Post 请求期间发生错误,

Flutter/Dart - 从外部类更新状态

Flutter sliver 持久化

如何对齐卡片小部件中的图标按钮?

容器在发布模式下显示灰色框(抖动)

.info/connected 这两个代码之间有什么区别吗?

Future.wait() 中的 futures 可以顺序调用吗?

为什么这个参数类型不能分配给参数类型?

如何在Flutter 的 initState 中初始化 Firestore 查询