我正在尝试建立一个聊天存储系统与firebase, sqlite和sqlBrite。这样做的目的是在不重新构建页面的情况下流式传输新消息。来自sqlBrite的流只是在setstate上重建。当键盘缩回时。
如何让流在保存时自动更新
db文档
///INSERT INTO DB
Future<int> insertNewMessage(String id, int result, BriteDatabase briteDb,
Map<String, dynamic> row) async {
messageList.add(id);
await ifexists(id, messageId, briteDb)
? print('message already In')
: result = await briteDb.insert(messageTable, row);
return result;
}
////STREAM MESSAGES
Stream<List<Map<String, dynamic>>> getMessageMapListbyId(
{String sendId, String receiveId, database}) async* {
try {
BriteDatabase briteDb = await database;
yield* briteDb.createQuery(messageTable,
distinct: false,
where:
' $senderId=? $receiverId = ? ',
whereArgs: [
sendId,
receiverId,
])});
供应商文档
///ADD MESSAGES
addMessageTodb(message) async {
await ldbH
.msg_insertMessage(
message.id, modelFuncs.messageMaping(message, msgFuncs))
.then((value) async {
await getMessageYieldBase(message.senderId, message.receiverId);
});}
///STREAM NEW DATA
getMessageYieldBase(senderId, receiverId) async* {
yield* ldbH.msg_getAllMessagesbyId(senderId, receiverId);}
ui端
StreamBuilder(
stream: messageStream.getMessageYieldBase(
widget._currentUserId, widget.receiver.uid),
builder: (context, AsyncSnapshot<dynamic> snapshot) {
var d = snapshot.data;
var newList = snapshot.hasData ? d.reversed.toList() : [];
return
ListView.builder(
reverse: true,
padding: EdgeInsets.all(10),
controller: widget._listScrollController,
itemCount: newList.length,
itemBuilder: (BuildContext context, int index) {
return DisplayMessage(
currentUserId: widget._currentUserId,
receiver: widget.receiver,
message: newList[index],
);
});
})
所以只有当页面以某种方式重建时,新的文本才会继续出现。如有任何帮助,不胜感激。
如果您遇到这个问题,请使用moor库sqlbrite将不起作用,但这是一个帮助链接....https://resocoder.com/2019/06/26/moor-room-for-flutter-tables-queries-fluent-sqlite-database/
Matt Rešetár详细解释了这将很容易实现…