[英]How to pass Stream to GroupedListView Flutter
I am currently working on the messaging section of my app.我目前正在处理我的应用程序的消息传递部分。 I am using a streambuilder and streams in conjunction with a GroupedListView so that the messages can be grouped according to date.
我将流构建器和流与 GroupedListView 结合使用,以便可以根据日期对消息进行分组。
However, I am having some problems because the GroupedListView takes a list as its 'elements' parameter.但是,我遇到了一些问题,因为 GroupedListView 将列表作为其“元素”参数。 And we know streams aren't necessarily lists.
而且我们知道流不一定是列表。 I have looked into converting streams to lists but I can't seem to find a solution.
我已经研究过将流转换为列表,但似乎找不到解决方案。
Here's what the code looks like:代码如下所示:
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)),
)
)
],
),
);
}
);
}
),
),
Is there a way to convert streams to lists so I can pass it to the "elements" parameter?有没有办法将流转换为列表,以便我可以将它传递给“元素”参数? Or do I need to take a different approach?
还是我需要采取不同的方法? I also came across this SO post but it's without an answer.
我也遇到了这个 SO 帖子,但没有答案。 But this is essentially my same problem as well: Example
但这本质上也是我同样的问题: 示例
I would thoroughly appreciate any help!我将非常感谢任何帮助!
I'm not sure if what you asked for is something that exists.我不确定你所要求的是否存在。 But what I would do is create a
Stream<List<_YourType_>>
and with the snapshot given, I would use the data
as my list.但我要做的是创建一个
Stream<List<_YourType_>>
并使用给定的快照,我将使用data
作为我的列表。
PS: If you initialize your StreamBuilder
like StreamBuilder<List<_YourType_>>(...
then your snapshot will be an AsyncSnapshot<List<_YourType_>>
and its data value will already be a List<_YourType_>
with no need to cast or anything! PS:如果您像
StreamBuilder<List<_YourType_>>(...
一样初始化StreamBuilder
,那么您的快照将是AsyncSnapshot<List<_YourType_>>
并且其数据值将已经是List<_YourType_>
无需强制转换或任何事物!
PS2: If I were you, I would look for the time package as it has a .date
getter for DateTime
or even create your own like: PS2:如果我是你,我会寻找时间包,因为它有一个用于
DateTime
的.date
获取器,甚至可以创建你自己的:
extension DateTimeExtension on DateTime {
DateTime get date => isUtc ? DateTime.utc(year, month, day) : DateTime(year, month, day);
}
Just so it's easier to get your dates without the time included.只是这样在不包括时间的情况下更容易获得您的日期。
You should probably declare your StreamBuilder as StreamBuilder<List>.您可能应该将您的 StreamBuilder 声明为 StreamBuilder<List>。 You will run into an error saying object cannot access resource otherwise.
您将遇到一个错误,指出对象无法访问资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.