Published in


Flutter: Run function repeatedly using CRON

There are times when you want to run some tasks periodically, and to do that we have two things in a flutter.

  1. Timer
  2. Cron

Timer: It’s running a task on given specific time intervals whether it is seconds, minutes, hours, etc…

import 'dart:async';

void main() {
const oneSec = const Duration(seconds: 1);

Timer.periodic(oneSec, (Timer timer) {
print("Repeat task every one second"); // This statement will be printed after every one second

Cron lib which will be running periodically, but there is a difference between Timer and Cron. Cron used for more complex time intervals.

Example: If a task needs to be run at a specific time of an hour or a day or month then this is very useful. It is also called a time-based job scheduler.

Let’s see the diagram for a better understanding. The below diagram has an asterisk(*) that represents a number that appears in a specific position.

Cron has a daemon thread that runs in the background all the time and this thread is responsible for scheduling background work.

import 'package:cron/cron.dart';main() {
var cron = new Cron();
cron.schedule(new Schedule.parse('*/3 * * * *'), () async {
print('every three minutes');
cron.schedule(new Schedule.parse('8-11 * * * *'), () async {
print('between every 8 and 11 minutes');

The above examples are taken from the repository which pretty well explains that the first ‘*’ represents minutes, similar for the hour and so on as shown in the diagram.

Another example of the hour would be Schedule.parse(* 1,2,3,4 * * *), This schedule will run every minute every day during the hours of 1 AM, 2 AM, 3 AM, and 4 AM.

“*” This represents all possible numbers for that position.


Schedule.parse(* * * * *): This will run the job each minute till it gets to stop.

Schedule.parse(0 * * * *) : This Cron job will run at minute zero, every hour (i.e. an hourly Cron job):

15 * * * * : This is also an minutes Cron job but runs at minute 15 (i.e. 00:15, 01:15, 02:15 etc.):

30 2 * * * [command]This will run once a day, at 2:30 am.

0 0 2 * * [command]This will run once a month, on the second day of the month at midnight (i.e. November 5th, 12:00am, December 5th, 12:00am, etc.):

0 * * * 1 [command]This will run on Mondays, every hour (i.e. 24 times in one day, but only on Mondays):

0,10,20 * * * * [command]You can use multiple intervals separated by commas. This will run three times every hour, at minutes 0, 10, and 20:

*/5 * * * * [command]A division operator is also used. This will run 12 times per hour, i.e. every 5 minutes:

0 5-10 * * * [command]Dash can be used to specify a range. This will run once every hour between 5:00 am and 10:00 am:

Few Examples where CRON job can be used

  • You can used it for auto log out which will logout user after user after specific intervals.
  • Erase cache data after specific intervals.
  • You can even perform something as simple as fetching your most recent data, after specific interval or given time.

for more reference https://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800




The fastest growing community which makes development easier

Recommended from Medium

Understanding The Concept Of URI

Presented by HyperDAO ✨

Program to find factorial of a number

Christopher Vitale , Author of “Networkologies: A Philosophy of Networks for a Hyperconnected Age —…

The Distributed Download

How to spruce up and speed up your process mapping

Rust Day 18: Number of 1 Bit

GitHub’s Actions v2 — Tips and Tricks

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jitesh Mohite

Jitesh Mohite

I am technology enthusiastic, want to learn things quickly and dive deep inside it. I always believe in developing logical things which makes impact on end user

More from Medium

Accessing REST APIs in Google Cloud Functions with Flutter

Flutter Offline Storage Using HIVE database

Creating Kampung Eats in Flutter: Day 1

Flutter Pagination With Caching data (SQLite database) — Part 2