flutter - 值更改时提供程序不更新自定义小部件
问题描述
我的提供程序用于自定义小部件内,该小部件用作ListTile
父小部件内。
提供者位于从 astream
中获取数据的 aFirebase database
中,当触发流时,它将新数据存储在提供者中作为 a 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
解决方案
只有当监听器为真时,小部件才会被重建。如果侦听器为 false,则在您调用 notifyListeners() 时不会重建小部件;
推荐阅读
- laravel - Laravel 语言切换器,路由设置
- aspose - 如何获取 StackedColumn 图表中列的动态宽度
- html - Vue.JS 不读取 json 数据
- mysql - 如何在 SQL 脚本中传递预定义的值?
- c# - wpf界面元素被ocx控件覆盖
- postgresql - Activemq postgresql 在 activemq_msgs 表中将消息详细信息和内容记录为 JSON
- c - 如何在 x86_64 中使用 clang-8 的“影子调用堆栈”功能?
- javascript - iOS 通用链接在 Twitter 应用程序中不起作用,但 android 应用程序链接有效
- php - 在 PHP 中使用 jquery ajax 提交条件表单元素
- node.js - 如何使用 Jest 和 Enzyme 在构造函数中测试组件参数