Home > Software design >  How to pause timer till procedure within finish in Flutter
How to pause timer till procedure within finish in Flutter

Time:12-09

I'm using timer for refreshing data from

timer = Timer.periodic(Duration(seconds: 60), (Timer t) async {
      await getList();
})

Its working fine. I want to pause timer until getList proc completed. The proc itself take 30 to 120 second.

So is their any option to pause timer?

Thanks in Advance.

CodePudding user response:

There is an open issue for your problem. Dart sdk doesn't provide pause or restart feature for Timer. You can use this package which does the job for you. Or use Stream.periodic() which was mentioned in the comment. Hope this helps.

CodePudding user response:

I think you will prefer this kind of solution.

  late Timer timer;
  @override
  void initState() {
    super.initState();
    callDataWithPause60Seconde();
  }
callDataWithPause60Seconde() {
    Future.doWhile(() async {
      print("Call getList()");
      // await getList();
      timer = await Future.delayed(Duration(seconds: 60));
      return true;
    });
  }

don't forget to cancel timer

  @override
  void dispose() {
    timer.cancel();
    // TODO: implement dispose
    super.dispose();
  }
  • Related