Flutter 控制initState()方法只执行一次

目的

首次进入页面执行initState()方法,后面再返回页面不再执行initState()方法。

解决方法

第一步:在继承的类后面加上with AutomaticKeepAliveClientMixin

class _OrderFinishPageState extends State<OrderFinishPage>
    with AutomaticKeepAliveClientMixin {
}

第二步:在extends State类中加入@override bool get wantKeepAlive => true;

@override
bool get wantKeepAlive => true;

第三步:在build中加入super.build(context);

@override
Widget build(BuildContext context) {
  super.build(context);
  return Container(
    child: orderList.length == 0 ? _defaultPage() : _orderList(),
  );
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容