Home > Enterprise >  Flutter timer doesn't work when changed from milliseconds to seconds
Flutter timer doesn't work when changed from milliseconds to seconds

Time:05-26

When I change the Duration from milliseconds to seconds the app doesn't load correctly. I get no error in the debug, no logs, nothing, but the app simply doesn't respond to user interaction anymore, it's totally delayed. With milliseconds everything is normal again.

        Timer.periodic(Duration(seconds: thisMaxSeconds), (timer) {
              code...
        }

CodePudding user response:

May I know the value passed in the second (thisMaxSeconds) parameter of the Duration? If it's too high then periodic code execution will be delayed.

CodePudding user response:

The likely cause here is that the thisMaxSeconds variable is set to 0. This causes the loop to run as quickly as possible, blocking UI interaction and updates.

Paste the following into dartpad to play around.

import 'dart:async';

void main() {
  Timer.periodic(Duration(seconds: 0), (timer) {
    print('Tick: ${timer.tick}');
    if(timer.tick == 5){
      print('Done');
      timer.cancel();
    }
  });
}
  • Related