加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flutter – 在dispose()之后调用的setState()

发布时间:2020-12-14 14:55:47 所属栏目:百科 来源:网络整理
导读:当我单击凸起的按钮时,时间戳显示出来.现在,如果我等待5秒,然后确认将发生此错误的时间 ?在dispose()之后调用的setState() 我确实在控制台中看到flutter如何更新父窗口小部件,但为什么呢? – 我什么都不做 – 我等了5秒钟! 这个例子在普通项目中可以使用,
当我单击凸起的按钮时,时间戳显示出来.现在,如果我等待5秒,然后确认将发生此错误的时间
?在dispose()之后调用的setState()

我确实在控制台中看到flutter如何更新父窗口小部件,但为什么呢? – 我什么都不做 – 我等了5秒钟!
这个例子在普通项目中可以使用,但是在我的项目中,这个项目要复杂得多,因为在我等待的时候,颤动会更新状态……我怎么了?有没有人猜测在我更复杂的项目而不是在一个简单的项目中,颤动是随机更新的?

[UPDATE]
我检查它是从我的TabBar和TabBarView所在的级别更新.
是否必须对tabbarview所需的“with TickerProviderStateMixin”做些什么?可能是因为它导致应用程序定期和随机刷新?

class DateTimeButton extends State<DateTimeButtonWidget> {
  DateTime selectedDate = new DateTime.now();

  Future initTimePicker() async {
    final TimeOfDay picked = await showTimePicker(
      context: context,initialTime: new TimeOfDay(hour: selectedDate.hour,minute: selectedDate.minute),);

    if (picked != null) {
      setState(() {
        selectedDate = new DateTime(selectedDate.year,selectedDate.month,selectedDate.day,picked.hour,picked.minute);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return new RaisedButton(
      child: new Text("${selectedDate.hour} ${selectedDate.minute}"),onPressed: () {
        initTimePicker();
      }
    );
  }
}

解决方法

在调用setState之前,只需检查窗口小部件的状态类的布尔属性“已挂载”.

if (this.mounted){
 setState((){
       //Your state change code goes here
 });
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读