0

I have used to Timer.periodic in flutter to run a function for every minnute

      final periodicTimer = Timer.periodic(
                  const Duration(minutes: 1),
                  (timer) {
              print('some code here');


    });

so the problem here is whenever the function is getting triggered

lets say i added this function in a second screen

once if i go to that page its creating one request

if i go multiple times to that page it is adding multiple timers

after everyminnute

"some code here "

is being printed as many times as I fire that page (which has that periodic timer function)

but is there any way where no matter how many times it is fired up it should just run that function for every minnute

tried various other methods used timer.cancel() [which will completely cancel the timer]

and followed various stackoverflow threads which i couldnt make it work

any insights or any other way to get this approach ?

John
  • 137
  • 2
  • 12
  • If you don't want multiple periodic `Timer`s, check if you have an existing periodic `Timer` before creating a new one. – jamesdlin Sep 05 '22 at 06:15
  • how to check ?? https://stackoverflow.com/questions/62078532/how-to-check-if-timer-is-active-before-creating-a-new-one followed this thread cant check using if loop – John Sep 05 '22 at 06:39
  • When you create a `Timer.periodic`, save that `Timer` to, say, a member variable declared with type `Timer?`, and then check that variable before creating a new one (e.g. `if (!_timer?.isActive) { _timer = Timer.periodic(...); }`) – jamesdlin Sep 05 '22 at 06:58
  • var periodicTimer = new Timer.periodic( const Duration(minutes: 1), (timer) {}); adding if loop above it is showing me error Local variable 'periodicTimer' can't be referenced before it is declared. – John Sep 05 '22 at 07:03
  • Timer ptimer; if (!ptimer.isActive) { ptimer = Timer.periodic(const Duration(seconds: 10), (timer) { print(timer.isActive); print(timer.tick); }); } else { print('okie'); } error in if loop error : [The non-nullable local variable 'ptimer' must be assigned before it can be used. Try giving it an initializer expression, or ensure that it's assigned on every execution path] – John Sep 05 '22 at 07:09
  • You can declare `late Timer ptimer;` if you really want. Or make it nullable, as I suggested already (`Timer? ptimer;`). But you probably don't want a local variable anyway and instead want a member variable or a global variable so that you maintain a reference to your `Timer` across function calls. – jamesdlin Sep 05 '22 at 07:13
  • I am facing the same problem. In my case, i have a game that runs a timer for each round. At the end of the first round I cancel the timer. But when i start the second round the timer works at double speed i.e. it counts 2 seconds for every one second duration. In the third iteration it counts 3 seconds for every second duration. This is my code: – WeSay Solutions Mar 19 '23 at 11:22
  • `code` Timer.periodic( const Duration(seconds: 1), (_timer) => setState( () { if (_progress == 1.0 || userScore.isComplete) { _timer.cancel(); debugPrint('Cancelling the Timer ${_timer.isActive}'); } else { _progress += 0.0075; // assumes 135 seconds is the max time userScore.incrementTimer(); debugPrint( 'In Timer ${userScore.timeTaken} and ${_timer.isActive}'); } }, ), ); `code` – WeSay Solutions Mar 19 '23 at 11:24

0 Answers0