How to create a scheduled service in Flutter

2,485

You could make use of the alarm manager package.

A simple implementation of the same would look like below.

import 'dart:async';

import 'package:android_alarm_manager/android_alarm_manager.dart';
import 'package:flutter/widgets.dart';

void doStuff() {
  print("do stuff every minute");
}

Future<void> main() async {
  final int periodicID = 0;

  // Start the AlarmManager service.
  await AndroidAlarmManager.initialize();

  runApp(const Center(
      child:
      Text('See device log for output', textDirection: TextDirection.ltr)));
  await AndroidAlarmManager.periodic(
      const Duration(minutes: 1), periodicID, doStuff,
      wakeup: true);
}
Share:
2,485
Erol Asan
Author by

Erol Asan

Solving problems for a living

Updated on December 12, 2022

Comments

  • Erol Asan
    Erol Asan over 1 year

    How can you create a scheduled service in Flutter, which will be triggered at a specific time every day, and it will run some code? It needs to work for both Android and IOS and even if the app is terminated.

  • Erol Asan
    Erol Asan almost 5 years
    Will this work for IOS as well? And when the app is terminated either by user or OS?