flutter run function every x amount of seconds

前端 未结 2 1595
不知归路
不知归路 2020-12-09 07:51

inside my flutter app I want to check my api every 10 seconds. I found this post to run a function every x amount of time and did the following:

class _MainP         


        
2条回答
  •  悲&欢浪女
    2020-12-09 08:42

    Use Cron lib which will be run periodically, but there is a difference between Timer and Cron,

    Timer: It's running a task on given specific time intervals whether it is seconds, minutes, or hours.

    Cron: It's used for more complex time intervals, eg: if a task needs to be run on a specific time of an hour. let's see the diagram

    The above diagram has an asterisk that represents a number that appears in a specific position.

    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.

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

提交回复
热议问题