listview - Flutter 从父级更新列表视图
问题描述
我有一个可以根据用户行为或后端响应更改的对象列表。
我创建StatefulWidget
了可以在我的应用程序的多个地方使用的
class EventList extends StatefulWidget {
final List<EventListModel> models;
EventList({Key key, this.models}) : super(key: key);
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _EventListState();
}
}
class _EventListState extends State<EventList> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return ListView(
padding: EdgeInsets.symmetric(vertical: 8.0),
children: widget.models.map((EventListModel model) {
return EventListItem(
model: model,
);
}).toList(),
);
}
}
我怎样才能ListView
从我实例化我的地方更新这个EventList
?谢谢!
PS我正在寻找类似notifyDataSetChanged()
Android的东西。
解决方案
当你List
在 parent 中修改 your 时Widget
,你必须调用setState()
. 孩子Widget
将得到更新List
。
推荐阅读
- java - 需要帮助在 Java 中使用子字符串方法
- asp.net - ASP.NET Core 3.1 + Angular 9 基于角色的身份验证
- vue.js - 如何通过 VueJS 中的操作访问商店?
- inno-setup - Inno 下载插件 - 可调整大小?
- bash - 通过调用父文件夹名称运行脚本
- apache-spark - 为什么 Spark Structured Streaming 作业中缺少来自 StreamingQueryListener 的 QueryProgressEvent 日志?
- node.js - 如何使我的 node.js 应用程序可公开访问?
- javascript - 为什么在这个 javascript 代码段提升问题上打印 10
- java - 为什么“not”运算符不起作用(布尔值)?
- javascript - 针对数据库验证数据列表,然后阻止表单提交