颤振:计时器无法停止 [英] Flutter: Timer can not stop

查看:85
本文介绍了颤振:计时器无法停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下.我尝试关闭此计时器.计时器无法关闭.我该怎么做才能关闭此计时器?我需要紧急帮助.

My code is below. I try to close this timer. timer could not close. What can I do to close this timer? I need help immediatly.

Timer timerHttp;

@override
void initState() {
    timerHttp = Timer.periodic(new Duration(seconds: 10), (timerHttp) async {
      print("girdi yine asdadasd");
      final response = await http.get(
        "http://xxxxxxxxxxxxxxxxxxxxxxxxx",
      );
      if (response.statusCode == 200) {
        setState(() {
          var responseJson = json.decode(response.body);
          cm100Datas = responseJson;
          saat = [];
          l1voltState = [];
          l2voltState = [];
          l3voltState = [];
          l1akimState = [];
          l2akimState = [];
          l3akimState = [];
          for (var item in cm100Datas) {
            saat.add(saatFormat.parse(item["saat"]));

            l1voltState.add(double.parse(item["l1volt"]));
            l2voltState.add(double.parse(item["l2volt"]));
            l3voltState.add(double.parse(item["l3volt"]));

            l1akimState.add(double.parse(item["l1akim"]));
            l2akimState.add(double.parse(item["l2akim"]));
            l3akimState.add(double.parse(item["l3akim"]));
          }
        });
        if (this.mounted) {
          setState(() {
            loadingHttp = false;
          });
        }
      }
    });
    super.initState();
  }
 @override
  void dispose() {
    timerHttp.cancel();
    super.dispose();
  }

它给出了以下错误.

════════小部件库捕获到异常═══════════════════════════════════引发了以下断言在完成小部件树时:'package:flutter/src/widgets/framework.dart':断言失败:第4263 pos 12:'_debugLifecycleState!= _ElementLifecycle.defunct':是不是真的.

════════ Exception caught by widgets library ═══════════════════════════════════ The following assertion was thrown while finalizing the widget tree: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4263 pos 12: '_debugLifecycleState != _ElementLifecycle.defunct': is not true.

推荐答案

此函数 disconnectFromServer(); 正在创建一些错误.这个错误影响了计时器.

this function disconnectFromServer(); was creating some error. And that error has effected the timer.

@override
void dispose() {
   disconnectFromServer();
   timerHttp.cancel();
   super.dispose();
}

我通过更改此功能位置解决了此问题.像下面这样.

I solved this problem by changing this function place. like this below.

@override
void dispose() {
   timerHttp.cancel();
   disconnectFromServer();
   super.dispose();
}

这篇关于颤振:计时器无法停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆