taskDaily static method
Run a task daily
Provide a name
for the function and a callback
to execute.
The function will execute every day.
You can also provide an endAt
date to stop the task from running.
You can also provide a frequency
to run the task weekly, monthly or yearly.
Example:
NyScheduler.taskDaily("myFunction", () {
print("This will execute every day");
});
The above example will execute every day.
Implementation
static taskDaily(String name, Function() callback, {DateTime? endAt}) async {
if (endAt != null && !endAt.isInFuture()) {
return;
}
String key = "${name}_daily";
String? lastTime = await readValue(key);
if (lastTime == null || lastTime.isEmpty) {
await _executeTaskAndSetDateTime(key, callback);
return;
}
DateTime todayDateTime = now();
DateTime lastDateTime = DateTime.parse(lastTime);
Duration difference = todayDateTime.difference(lastDateTime);
bool canExecute = (difference.inDays >= 1);
if (canExecute) {
// set the time
await _executeTaskAndSetDateTime(key, callback);
}
}