1

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();
  }

it gives this below error.

════════ 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.

SefaUn
  • 824
  • 1
  • 7
  • 23
  • Can you please explain what are you doing? Like, why are you calling the API every 10 seconds? – Ravi Singh Lodhi Dec 15 '20 at 06:00
  • I refresh my data list every 10 second. Real problem, when I want to leave this page, timer can not close. @RaviSinghLodhi – SefaUn Dec 15 '20 at 06:02
  • I think the problem is here: if (this.mounted) { setState(() { loadingHttp = false; }); } – Akif Dec 15 '20 at 06:11
  • I solved this probem right now. I was using a function in dispose before `timerHttp.cancel()`. That function has been create a error. that error had effected timer and timer hadn't stoped. I changed this function place. Timer stoped right now. thanks all answer. by the way it is bullshit problem :) @akif , @RaviSinghLodhi – SefaUn Dec 15 '20 at 06:17
  • How did you change the place of timerHttp.cancel method? Where did you add it? Can you paste the new code? – Akif Dec 15 '20 at 06:24
  • I paste below this error of question. @akif – SefaUn Dec 15 '20 at 06:28

1 Answers1

0

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();
}
SefaUn
  • 824
  • 1
  • 7
  • 23
  • Ok, right now we need disconnectFromServer() method too. Next time, if you want an accepted answer you need to add the whole code for us to understand the issue. Good luck! – Akif Dec 15 '20 at 06:31