首页 > 解决方案 > 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的东西。

标签: listviewflutterflutter-layout

解决方案


当你List在 parent 中修改 your 时Widget,你必须调用setState(). 孩子Widget将得到更新List


推荐阅读