Last active
July 4, 2023 10:06
-
-
Save DanielRomanMartinez/73980ad130380cf80f20fbb36e21a8ec to your computer and use it in GitHub Desktop.
DateTime Delay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
void main() { | |
print('${DateTime.now()} - First'); | |
executeOnMinute(() => print('${DateTime.now()} - Hello')); | |
} | |
void executeOnMinute(void Function() callback) { | |
DateTime now = DateTime.now(); | |
DateTime nextHourToSalutate; | |
if((now.hour >= 8) && now.hour < 12){ | |
nextHourToSalutate = DateTime( | |
now.year, | |
now.month, | |
now.day, | |
12, | |
0, | |
0, | |
); | |
} else if(now.hour >= 12 && now.hour < 19){ | |
nextHourToSalutate = DateTime( | |
now.year, | |
now.month, | |
now.day, | |
19, | |
0, | |
0, | |
); | |
} else { | |
nextHourToSalutate = DateTime( | |
now.year, | |
now.month, | |
now.day + 1, | |
8, | |
0, | |
0, | |
); | |
} | |
Future.delayed(nextHourToSalutate.difference(now), () { | |
callback.call(); | |
executeOnMinute(callback); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment