Flutter Cron Job Scheduler
Flutter Cron Job Scheduler

Hi Guys, Welcome to Proto Coders Point, In this flutter tutorial article, we will learn how to call a function at a specific interval of time.

There are some situation when we want to run a function every second, minute, hour, day or once a week to execute a task periodically and to do so we can make use of flutter cron job package.

Flutter Cron Job Scheduler

Cron Job in flutter are used to run function at certain time interval.

For Example: Suppose you want to call function every 1 minute, hour, day or month then flutter cron job libray is very useful its also called as time based job scheduler.


How to use cron flutter package

1. Flutter installing cron package

In your flutter project open pubspec.yaml file & under dependencies add cron.

dependencies:
  cron: # keep version empty for latest version

After addmin cron plugin, hit pub get button or run flutter pub get cmd in terminal, to downlaod & install cron in flutter project.


2. Import cron

Now, once the job scheduler package is successfully installed, import it in you flutter dart code to use it.

import 'package:cron/cron.dart';

3. How to use cron in flutter as job scheduler

main() {
  final cron = Cron();
  cron.schedule(Schedule.parse('*/5 * * * *'), () async {
    print('every Five minutes');
  });
  runApp(myApp) ;
}

Example 2

main() {
  final cron = Cron();
  cron.schedule(Schedule.parse('*/5 * * * * *'), () async {
    print('Runs every Five seconds');
  });
  runApp(myApp) ;
}

Syntax for job scheduler using cron in flutter

fieldvalue
second0-59
minute0-59
hour0-23
day of month1-31
month1-12
day of week0 – 7

How to stop cron job

To stop cron job simple use .close

await cron.close()


You can use cron job when you want to trigger local notification in flutter at a specific time internal