当我单击凸起的按钮时,时间选择器就会出现。现在,例如,如果我等待 5 秒,然后确认时间,就会出现此错误:setState() 在 dispose() 之后调用
我确实在控制台中看到 flutter 如何更新父 widget,但为什么呢?我什么也没做——我只是等5秒?!
下面的示例将在普通项目中工作,但是在我的项目中相当复杂,它不起作用,因为 Flutter 在我等待时正在更新状态......我做错了什么?有谁猜测 Flutter 在我的更复杂的项目中而不是在简单的项目中随机更新可能是什么?
[UPDATE]我又看了一眼,发现它正在从我所在的级别更新TabBar
and TabBarView
是。
它是否必须与“与 TickerProviderStateMixin“我需要的是TabBarView
?难道是导致app定时随机刷新?
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();
}
);
}
}
只需检查布尔属性mounted
调用之前小部件的状态类setState()
.
if (this.mounted) {
setState(() {
// Your state change code goes here
});
}
或者更干净的方法
覆盖setState
方法在你的StatelfulWidget
class.
class DateTimeButton extends StatefulWidget {
@override
void setState(fn) {
if(mounted) {
super.setState(fn);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)