我目前正在开发我的应用程序的聊天功能。 我在 StreamBuilder 中设置了一个 AnimatedList,以便使消息反向显示。 这是我的代码
children: <Widget>[
new Flexible(
child: new StreamBuilder<QuerySnapshot> (
stream: chatRoomRef.document(widget.chatRoomID).collection('messages')
.orderBy('time').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
return new AnimatedList(
reverse: true,
padding: const EdgeInsets.all(8.0),
itemBuilder: (BuildContext context, int index, Animation<double> animation) {
return new ChatMessageListItem(
context: context,
index: index,
animation: animation,
reference: snapshot,
);
}
);
},
),
),
我的问题是构建器永远不会被命中,所以永远不会调用 AnimatedList。我不确定设置是否正确,因此非常感谢任何帮助。
编辑: 我试图让它像 FirebaseAnimatedList 小部件一样工作。我不知道这是否有助于理解我的目标。
谢谢
最佳答案
尝试验证你的快照是否有数据
children: <Widget>[
new Flexible(
child: new StreamBuilder<QuerySnapshot> (
stream: chatRoomRef.document(widget.chatRoomID).collection('messages')
.orderBy('time').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
return snapshot.hasData ? new AnimatedList(
reverse: true,
padding: const EdgeInsets.all(8.0),
itemBuilder: (BuildContext context, int index, Animation<double> animation) {
return new ChatMessageListItem(
context: context,
index: index,
animation: animation,
reference: snapshot,
);
}
): new CircularProgressIndicator();
},
),
),
关于dart - 带有嵌套 AnimatedList 的 Firestore StreamBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51681823/