As I inform a condition for when the screen is changed the timer.Eriodic cancel

Asked

Viewed 54 times

-1

Today I use so with the code below, but it doesn’t work very well, because when changing screen the query continues to be executed in background.

Future _refresh() async {
  if (_statusLote != "finalizado") {
    _timer = Timer.periodic(Duration(seconds: 30), (timer) {
      print("Com atualizações");
      print(_statusLote);
      setState(() {
        pegarCarcacas();
      });
    });
  } else {
    print("Sem atualizações");
    print(_statusLote);
    closeTimer();
  }
}
  • Since it’s a new user, it’ll be a tip for us to help you better. Edit your question by mounting the full scenario of your problem by clearly explaining your code you propose to solve. Then enter functional codes so that members of the community can understand and help you better. Also mention what you have done of attempts to resolve if you have tried anything. Do not be afraid to detail, as long as the information helps in understanding, details are very welcome.

  • If that’s what I understand, your problem is that when you leave the screen where you have this snippet of code the app should stop making queries. Is that right? If it is within this Timer that queries occur, wouldn’t there be some process in your code that you can use to pause it when directed to the next screen? Anyway, with the details it’ll be easier to help you.

  • So for my needs I need a new query and the return of it along with the screen update to be updated every 30s, until ok. Problem and when the user leaves this screen, the query continues to be performed. The return of the query brings a status information that is changed in my backend, however this change can take up to hours, it would not be appropriate to wait only this event occur for the consultations sessarem. I really want to solve this unexpected error of the query that is running even if it is not on the screen where it was launched.

  • Solved: I removed my _refresh() function from Build and added it to initstate(). Also I involved the build in a Willpopscope, with this I can lock the back button and run the cancellation of Periodic time. Thank you all for your attention.

  • I’m glad you solved your problem. Now that you have your solution, I recommend that you post it in response format so it can be better used in the future. Another point I recommend is about the "chopped" codes, because from what I noticed, your question did not make clear how the structure of your class was and this directly affects the suggestions for correction. If you decide to post the solution put your full class in the answer.

  • @Weberthmoreira answer your question with the way you solved there, this can help anyone who has the same problem in the future...

Show 1 more comment

1 answer

0

Try to change your variable Timer local to a global variable within its class and treat it on dispose of your class, below is an example:

class YellowBird extends StatefulWidget {
  const YellowBird({ Key key }) : super(key: key);

  @override
  _YellowBirdState createState() => _YellowBirdState();
}

class _YellowBirdState extends State<YellowBird> {

  Timer _timer;

  @override
  dispose(){
    closeTimer();
    super.dispose;
  }

  void closeTimer(){
    if (_timer.isActive)
      _timer.cancel();
  }


  Future _refresh() async {
    if (_statusLote != "finalizado") {
      _timer = Timer.periodic(Duration(seconds: 30), (timer) {
          print("Com atualizações");
          print(_statusLote);
          setState(() {
            pegarCarcacas();
          });
        });
    } else {
      print("Sem atualizações");
      print(_statusLote);
      closeTimer();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(color: const Color(0xFFFFE306));
  }
}
  • Same situation and presents after @Matheus Ribeiro’s suggestion.

  • @Weberthmoreira advise that in the next questions, you inform as much data as possible, if you had said that your function was called within the build() we could have helped you in a matter of minutes!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.