How to run Future/await with Timer.periodic in flutter

240 Views Asked by At

i want running next step

  1. mainTimerStart run
  2. after 2sec
  3. print 'Fist Timer End'
  4. mainTimerStart run
  5. after 3sec
  6. print 'Second Timer End'

But.... now All the step work at once.

look at my code

button function code

onPressed: () async {
  await timeSetController.mainTimerStart(2);
  print('First Timer End');
  await timeSetController.mainTimerStart(3);
  print('Second Timer End');
},

mainTimerStart function code

Future<void> mainTimerStart(int setMainTime) async {
  isMainTimerRun(true);
  time(setMainTime);
  mainTimer = Timer.periodic(const Duration(seconds: 1), (timer) async {
    if (time <= 1) {
      mainTimer?.cancel();
      time(0);
      isMainTimerRun(false);
    } else {
      time--;
    }
  });
}
0

There are 0 best solutions below