【发布时间】:2021-11-22 12:02:50
【问题描述】:
我的提供程序在一个自定义小部件内使用,该小部件在父小部件内用作ListTile。
提供程序位于stream 内部,从Firebase database 获取数据,当触发流时,它将新数据存储在提供程序内部作为Map。
提供者称为LastMessage,Map 称为messageMap,使用函数updateMap 添加新数据:
var messageInstance = Provider.of<global.LastMessage>(context, listen: false);
StreamSubscription<Event> updates;
// roomId is a string that is used as a key
updates = ref.child(RoomId).onChildAdded.listen((event) async{ // the stream works correctly and the new data gets stored
String _x = await getLastMessage();// a function that gets the data from the database
messageInstance.updateMap(RoomId, _x);
});
class LastMessage extends ChangeNotifier{
Map<String, String> messageMap = {};
void updateMap(String RoomID, String lastMessage){
messageMap[RoomID] = lastMessage;
notifyListeners();
}
现在的问题是,即使在 messageMap 更新后,当 listen 设置为 false 时,小部件也不会重建,尽管它在 listen 为 true 时有效,但它使整个应用程序很慢
【问题讨论】:
标签: flutter dart flutter-provider