Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DanielRomanMartinez/73980ad130380cf80f20fbb36e21a8ec to your computer and use it in GitHub Desktop.
Save DanielRomanMartinez/73980ad130380cf80f20fbb36e21a8ec to your computer and use it in GitHub Desktop.
DateTime Delay
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