我正在使用Firestore开发一个聊天应用,每次聊天中添加新消息时,小部件都会不断重建。我认为这是因为StreamBuilder。 StreamBuilder是否有替代方案?
聊天中有一个功能,用户可以发送语音消息。问题是聊天中收到新消息时语音消息停止播放。我的观察是,我认为这是由于重建小部件引起的。
-请原谅我的英语不好
这是我的代码。
PublishSubject<DocumentSnapshot> _chatItemsSnapshotSubject = PublishSubject();
Stream<DocumentSnapshot> get _chatItemsStream =>
_chatItemsSnapshotSubject.stream;
@override
void initState(){
_db
.collection('groupchats')
.document(globals.groupSelected)
.collection('messages')
.orderBy('created_at', descending: true)
.limit(20)
.snapshots()
.listen((onData) {
_chatItemsSnapshot = onData.documents;
onData.documents.forEach((snapshots) {
_chatItemsSnapshotSubject.add(snapshots);
});
});
}
//code inside build()
StreamBuilder(
stream: _chatItemsStream,
builder: ((context,AsyncSnapshot<DocumentSnapshot> snapshot) {
if(!snapshot.hasData){
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text("Loading...",
style: TextStyle(color: Colors.white)),
)
],
)
);
}
return ListView.builder(
controller: _chatScrollController,
itemCount: _chatItemsSnapshot.length,
reverse: true,
itemBuilder: (context,index){
return ListItemWidget();
}
)
})
)