| title | Quick Start |
|---|---|
| description | Get started with Flutter Workmanager in minutes |
Add workmanager to your pubspec.yaml:
dependencies:
workmanager: ^0.8.0Then run:
flutter pub getAndroid works automatically - no additional setup required! ✅
iOS requires a 5-minute setup in Xcode. Choose your approach based on your needs:
For regular data sync, notifications, cleanup - uses iOS Background Fetch:
- Enable Background Modes in Xcode target capabilities (Configuration Guide) and add to Info.plist (UIBackgroundModes reference):
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
</array>- No AppDelegate configuration needed - works automatically from Dart code
For file uploads, data processing, longer tasks - uses BGTaskScheduler:
- Enable Background Modes in Xcode target capabilities (Configuration Guide) and add to Info.plist (UIBackgroundModes reference):
<key>UIBackgroundModes</key>
<array>
<string>processing</string>
</array>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.yourapp.processing_task</string>
</array>- Configure AppDelegate.swift (required for BGTaskScheduler):
import workmanager_apple
// In application didFinishLaunching
WorkmanagerPlugin.registerBGProcessingTask(
withIdentifier: "com.yourapp.processing_task"
)For periodic tasks with more control than Background Fetch - uses BGTaskScheduler with frequency:
- Enable Background Modes in Xcode target capabilities (Configuration Guide) and add to Info.plist (UIBackgroundModes reference):
<key>UIBackgroundModes</key>
<array>
<string>processing</string>
</array>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.yourapp.periodic_task</string>
</array>- Configure AppDelegate.swift (with frequency control):
import workmanager_apple
// In application didFinishLaunching
WorkmanagerPlugin.registerPeriodicTask(
withIdentifier: "com.yourapp.periodic_task",
frequency: NSNumber(value: 20 * 60) // 20 minutes (15 min minimum)
)@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
switch (task) {
case "data_sync":
await syncDataWithServer();
break;
case "cleanup":
await cleanupOldFiles();
break;
case Workmanager.iOSBackgroundTask:
// iOS Background Fetch task
await handleBackgroundFetch();
break;
default:
// Handle unknown task types
break;
}
return Future.value(true);
});
}import 'package:flutter/foundation.dart';
void main() {
Workmanager().initialize(callbackDispatcher);
runApp(MyApp());
}// Schedule a one-time task
Workmanager().registerOneOffTask(
"sync-task",
"data_sync",
initialDelay: Duration(seconds: 10),
);
// Schedule a periodic task
Workmanager().registerPeriodicTask(
"cleanup-task",
"cleanup",
frequency: Duration(hours: 24),
);Your background tasks can return:
Future.value(true)- ✅ Task successfulFuture.value(false)- 🔄 Task should be retriedFuture.error(...)- ❌ Task failed
- Callback Dispatcher: Must be a top-level function (not inside a class)
- Separate Isolate: Background tasks run in isolation - initialize dependencies inside the task
- Platform Differences:
- Android: Reliable background execution, 15-minute minimum frequency
- iOS: 30-second limit, execution depends on user patterns and device state
- Task Customization - Advanced configuration with constraints, input data, and management
- Debugging Guide - Learn how to debug and troubleshoot background tasks
- Example App - Complete working demo