Home > front end >  Periodic timer disposal not working in Flutter
Periodic timer disposal not working in Flutter

Time:08-19

I have a periodic timer of 5 seconds, where I am calling a function.

timer3 = Timer.periodic(
      Duration(seconds: 5),
      (Timer t) => function());

The function is as below;

 Future<dynamic> function() async {
    var url = "https://rtdjiigwvh.execute-api.us-east-2.amazonaws.com/v1/preview_checkout";
    Map data = {};
    String body = json.encode(data);
        var res = await http
        .post(url, headers: {"Content-Type": "application/json"}, body: body)
        .then((value) {
      location = json.decode(value.body)["Location"].toString();
      print(location);
       if(location == 'ENTRY')
      setState(() {
              entry = true;
            });
      else if(location == 'EXIT')
      setState(() {
              exit = true;
              @override
              void dispose() {
              timer3?.cancel();
              super.dispose();
             }
            });
            if(exit == true)
            Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) =>
                NextScreen()));
    });
  }

Here when the boolean variable exit is true, I want the screen to redirect to the next screen, and also want to stop the polling which I am doing using the timer. Therefore I have used the dispose function for the timer to stop, but the problem is it won't stop right there and keeps on polling and refreshing the NextScreen continuously for every 5 seconds. Kindly help me resolve this issue. Thanks in advance!

CodePudding user response:

Dispose method should be defined outside of function method. Move:

@override
void dispose() {
    timer3?.cancel();
    super.dispose();
}

outside and it will be automatically called when the statefulwidget gets disposed.

CodePudding user response:

You can use

Future.delayed(Duration(seconds : 5)).then((){
  function();
});

instead of timer

CodePudding user response:

Use This :

    Future<dynamic> function() async {
        var url = "https://rtdjiigwvh.execute-api.us-east-2.amazonaws.com/v1/preview_checkout";
        Map data = {};
        String body = json.encode(data);
            var res = await http
            .post(url, headers: {"Content-Type": "application/json"}, body: body)
            .then((value) {
          location = json.decode(value.body)["Location"].toString();
          print(location);
           if(location == 'ENTRY')
          setState(() {
                  entry = true;
                });
          else if(location == 'EXIT')
          setState(() {
                  exit = true;
                  @override
                  void dispose() {
                  timer3?.cancel();
                  super.dispose();
                 }
                });
                if(exit == true)
               {
timer3?.cancel();

 Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) =>
                    NextScreen()));
}
        });
      }
  • Related