Why anonymous functions do not work as Flutter alarm callbacks
Passing a parameter into an android_alarm_manager callback with a closure silently does nothing. Use a top-level function that takes the value as an argument instead.

A question on Stack Overflow asked why a Flutter alarm never fired once the callback was written as an anonymous function. The version with the function passed directly works. The version with the parameter captured in a closure does not. My answer there has the working code, and this note explains the same thing in more detail.
The code and versions below are from January 2020 (Flutter 1.13,
android_alarm_manager0.4.5). Plugin APIs change, so check the current documentation before you reuse them.
The problem
The working call passes a named function:
AndroidAlarmManager.oneShotAt(DateTime.now().add(Duration(seconds: 5)), int.parse(id), alarmCallback); // works
with a plain top-level function:
void alarmCallback(String id) {
print("Hello, " + id);
}
To pass the id through, the question wrapped the call in a closure:
AndroidAlarmManager.oneShotAt(DateTime.now().add(Duration(seconds: 5)), int.parse(id), () => alarmCallback(id)); // does not work
Nothing happens. There is no exception, and the callback is never executed.
Why it fails
This is my understanding. Please check it against the plugin documentation.
The alarm callback does not run in your app’s main isolate. It runs in a separate background isolate, which can start even when the app is not in the foreground. To find the function in that isolate, the plugin stores a callback handle that refers to the function by its place in the code. Flutter can only create such a handle for top-level or static functions. A closure captures local values (here id) and has no stable name to look up, so the plugin cannot register it.
The fix is to not capture anything. Give the callback a parameter and pass the value as the alarm’s ID, which the plugin hands to the function when the alarm fires.
A working example
This code sets up a periodic alarm and a one-time alarm. The callback takes an int, which is the alarm’s ID.
import 'package:flutter/material.dart';
import 'package:android_alarm_manager/android_alarm_manager.dart';
import 'dart:isolate';
void callbackFunction(int id) async {
final DateTime now = DateTime.now();
final int isolateId = Isolate.current.hashCode;
print('[$now] Param=$id isolate=$isolateId function=$callbackFunction');
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
AndroidAlarmManager.initialize();
runApp(MyApp());
final int alarmID = 42;
await AndroidAlarmManager.periodic(const Duration(minutes: 1), alarmID,
callbackFunction, wakeup: true, exact: true, rescheduleOnReboot: true)
.then((val) => print('periodic alarm set up:' + val.toString()));
final int anotherAlarmID = 666;
AndroidAlarmManager.oneShot(
Duration(seconds: 3),
anotherAlarmID,
callbackFunction,
wakeup: true,
).then((val) => print('oneShot alarm set up:' + val.toString()));
}
The alarm ID is the value the callback receives. That is why the parameter is an int here. The question stored the ID as a String, so convert it once with int.parse(id) before scheduling and pass the number, not a closure.
The manifest needs the boot and wake-lock permissions and the plugin’s service and receivers:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.full.flutter_app_new">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:name="io.flutter.app.FlutterApplication"
android:label="flutter_app_new"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name="io.flutter.plugins.androidalarmmanager.AlarmService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"/>
<receiver
android:name="io.flutter.plugins.androidalarmmanager.AlarmBroadcastReceiver"
android:exported="false"/>
<receiver
android:name="io.flutter.plugins.androidalarmmanager.RebootBroadcastReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
When it works, the log looks like this. The callback runs in its own isolate, so the isolate ID differs from the main app’s:
I/FlutterBackgroundExecutor(12111): Starting AlarmService...
I/AndroidAlarmManagerPlugin(12111): onAttachedToEngine
I/flutter (12111): periodic alarm set up:true
I/flutter (12111): oneShot alarm set up:true
I/AlarmService(12111): AlarmService started!
I/flutter (12111): [2020-01-07 00:05:33.065838] Param=666! isolate=470178969 function='Closure: (int) => void from Function 'callbackFunction': static.'
I/flutter (12111): [2020-01-07 00:06:26.158709] Param=42! isolate=470178969 function='Closure: (int) => void from Function 'callbackFunction': static.'
Keep in mind: alarms are not exact
Since Android API 19 (KitKat), the system can delay alarms to save battery. exact: true asks for exact timing, but the system may still deviate. For details see the Stack Overflow answer on alarm precision and the AlarmManager documentation.