Flutter 周期计时器无法处理
代码内容并不重要。当我想离开此页面时,只有一个问题计时器无法处理。当我离开此页面时,Flutter 周期计时器无法处理,flutter,dart,Flutter,Dart,代码内容并不重要。当我想离开此页面时,只有一个问题计时器无法处理。当我离开此页面时,sendMessage(“message”)功能继续运行。是否有处置此计时器的选项 Timer timer; @override void initState() { super.initState(); timer = Timer.periodic(new Duration(seconds: 5), (timer) async { setState(() { uni
sendMessage(“message”)代码>功能继续运行。是否有处置此计时器的选项
Timer timer;
@override
void initState() {
super.initState();
timer = Timer.periodic(new Duration(seconds: 5), (timer) async {
setState(() {
unicode++;
unicodeString = unicode.toString();
if (unicodeString.length < 6) {
int different = 6 - unicodeString.length;
for (var i = 0; i < different; i++) {
unicodeString = "0" + unicodeString;
}
}
sendMessage("meesage");
showSnackBarWithKey("Message Sended !");
});
});
}
@override
void dispose() {
timer.cancel();
super.dispose();
}
定时器;
@凌驾
void initState(){
super.initState();
计时器=计时器。周期(新持续时间(秒:5),(计时器)异步{
设置状态(){
unicode++;
unicodeString=unicode.toString();
如果(最小长度<6){
int-different=6-unicode.length;
对于(变量i=0;i<不同;i++){
unicodeString=“0”+unicodeString;
}
}
sendMessage(“meesage”);
showSnackBarWithKey(“消息已发送!”);
});
});
}
@凌驾
无效处置(){
timer.cancel();
super.dispose();
}
错误如下
WIDGETS库捕获到异常
完成小部件树时引发了以下断言:
“package:flatter/src/widgets/framework.dart”:失败的断言:第4182行位置12:
"生命周期屋!_ElementLifecycle.defunct':不正确。
要么断言表明框架本身存在错误,要么我们应该提供
此错误消息中的更多信息可帮助您确定并修复根本原因。
在任何一种情况下,请通过在GitHub上提交bug来报告此断言:
我使用了dispose定时器,但它不能处理定时器。我解决不了这个问题。请提供帮助。运行代码后,我发现没有问题,
主要问题是,,
当小部件从父树中完全删除时,将对其调用dispose
因此,当您发送新页面时
使用推送导航,将在当前屏幕上添加一个新屏幕
屏幕。因此,(旧屏风的)树并没有被完全摧毁
因此,不调用dispose
使用流行音乐。屏幕被移除,树也被移除。因此处置是必要的
打电话来
使用推送更换。新屏幕替换旧屏幕删除
小部件树。这就是所谓的
至于代码,
试试这个。
(主要部件是推送更换我将其用于导航)
最后的代码是
class TimerButton extends StatefulWidget {
@override
_TimerButtonState createState() => _TimerButtonState();
}
class _TimerButtonState extends State<TimerButton> {
Timer _timer;
@override
void initState() {
super.initState();
_timer = Timer.periodic(new Duration(seconds: 5), (timer) async{
setState(() {
/* unicode++;
unicodeString = unicode.toString();
if (unicodeString.length < 6) {
int different = 6 - unicodeString.length;
for (var i = 0; i < different; i++) {
unicodeString = "0" + unicodeString;
}
}*/
sendMessage("meesage");
showSnackBarWithKey("Message Sended !");
});
});
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return RaisedButton(
onPressed: (){
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => SplashScreen()));
},
child: Text("data"),
);
}
}
class TimerButton扩展StatefulWidget{
@凌驾
_TimerButtonState createState();
}
类_TimerButtonState扩展状态{
定时器(u定时器),;
@凌驾
void initState(){
super.initState();
_计时器=计时器。周期(新持续时间(秒:5),(计时器)异步{
设置状态(){
/*unicode++;
unicodeString=unicode.toString();
如果(最小长度<6){
int-different=6-unicode.length;
对于(变量i=0;i<不同;i++){
unicodeString=“0”+unicodeString;
}
}*/
sendMessage(“meesage”);
showSnackBarWithKey(“消息已发送!”);
});
});
}
@凌驾
无效处置(){
_timer.cancel();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
//TODO:实现构建
返回上升按钮(
已按下:(){
导航器。更换(
context,MaterialPageRoute(builder:(context)=>SplashScreen());
},
子项:文本(“数据”),
);
}
}
而不是dispose()
尝试将其放入停用()
我希望上述解决方案对您适用,但如果不适用,您也可以尝试以下代码,因为在我的情况下,上述解决方案不适用。
static Timer timerObjVar;
static Timer timerObj;
timerObj = Timer.periodic(Duration(seconds: 10), (Timer timer) async {
timerObjVar = timer;
_initData();
});
// when you want to cancel the timer call this function
cancelTimer() {
if (timerObjVar != null) {
timerObjVar.cancel();
timerObjVar = null;
}
if (timerObj != null) {
timerObj.cancel();
timerObj = null;
}
}
重要的是提供一个最小的完整的可复制代码,以便在该问题上运行测试。您确定没有多次创建周期性计时器吗?是的,我确定,我只是在initState()中创建了一个周期性计时器
@jamesdlinAndtimer
是实例变量还是全局变量?从你发布的代码无法判断。另外,请确认只创建了一个计时器
,而不是依赖于我想离开此页面时可能出现的错误假设。-你能提供更多关于你如何离开这个页面的信息吗?
static Timer timerObjVar;
static Timer timerObj;
timerObj = Timer.periodic(Duration(seconds: 10), (Timer timer) async {
timerObjVar = timer;
_initData();
});
// when you want to cancel the timer call this function
cancelTimer() {
if (timerObjVar != null) {
timerObjVar.cancel();
timerObjVar = null;
}
if (timerObj != null) {
timerObj.cancel();
timerObj = null;
}
}