搜索插件、代码仓与 APP 分类

插件详情v0.10.10

workmanager

Flutter Workmanager。此插件允许您在 Android 和 iOS 上安排后台工作。

AndroidiOS
近 30 天下载
35.3万
喜欢数
2430
Pub 分数
140

安装插件

flutter pub add workmanager

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Workmanager 是一个 Flutter 插件,可在 Android 和 iOS 平台上实现后台任务调度。它允许开发者在应用终止时仍能运行周期性或一次性任务,非常适合用于数据同步、获取更新或后台文件处理等任务。该插件与原生后台执行服务集成,确保在系统限制内可靠地执行任务。它支持前台和后台模式,并可配置执行间隔和条件。

适合场景

  • 计划数据同步
  • 背景文件处理
  • 定期API轮询
  • 推送通知处理
  • 电池效率高的任务执行

主要特点

  • 跨平台后台任务
  • 对周期性任务和一次性任务的支持
  • 可配置的执行间隔
  • 与原生操作系统后台服务的集成
  • 低电量和电源优化

适合对象

  • 需要可靠后台执行的应用程序
  • 数据密集型应用
  • 离线优先的应用程序
  • 无需用户交互即可定期更新的应用程序
  • 同时针对 Android 和 iOS 的开发者

使用建议

  • 由于iOS的后台执行策略更严格,功能受限
  • 需要在 Xcode 中正确配置后台模式
  • 可能受设备省电功能限制
  • 不适合用于时间敏感的任务
  • 需要在真实设备上进行测试以确保可靠性

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Flutter Workmanager

pub package pub points likes popularity GitHub Workflow Status license

Execute Dart code in the background, even when your app is closed (Android and iOS). On macOS, background tasks run while the app is running or backgrounded and the Mac is awake. Perfect for data sync, file uploads, and periodic maintenance tasks.

⚠️ Experimental web support now exists via workmanager_web (Service Worker + Web Worker based background execution). See the Web (experimental) page and the package README for honest limitations and how to test it in Chrome.

📖 Documentation

Get started with background tasks in Flutter:

→ Quick Start Guide - Installation and setup

→ API Documentation - Complete Dart API reference

→ Debugging Guide - Troubleshooting help

→ Troubleshooting Guide - Why tasks stop after app close (OEM battery optimization, constraints, verification)

🎯 Use Cases

Background tasks are perfect for:

  • Sync data from API - Keep your app's data fresh
  • Upload files in background - Reliable file uploads
  • Clean up old data - Remove old files and cache
  • Fetch notifications - Check for new messages
  • Database maintenance - Optimize and clean databases

⏱ Long-Running Tasks (Android)

For work that takes longer than the regular background execution window (bulk uploads/downloads, ML processing), Android tasks can run as a foreground service with a persistent notification. Pass a foregroundServiceConfig to registerOneOffTask or registerPeriodicTask:

await Workmanager().registerOneOffTask(
  "upload-task",
  "upload_files",
  foregroundServiceConfig: ForegroundServiceConfig(
    notificationTitle: "Uploading files",
    notificationText: "Your files are being uploaded",
  ),
);

See the Quick Start guide for details and platform caveats.

🛑 Cancelling Running Work (Android)

cancelByUniqueName, cancelByTag and cancelAll stop the WorkManager worker immediately, but the Dart callback that is currently running keeps executing unless it reacts to the stop. Pass an onTaskStopped handler to executeTask inside your callbackDispatcher to be notified when a running task is stopped before it finishes:

@pragma('vm:entry-point')
void callbackDispatcher() {
  Workmanager().executeTask(
    (taskName, inputData) async {
      return true;
    },
    onTaskStopped: (taskName, stopReason) async {
      // Mark the task as cancelled / persist state, then return promptly.
    },
  );
}

The handler receives the task name and the WorkManager stop reason (cancelledByApp, timeout, preempt, ...); on Android < 12 the reason is StopReason.unknown. Android-only — iOS has no way to stop a running task. See the Customization guide for details.

🐛 Issues & Support

🚀 Example App

See the example folder for a complete working demo with all task types and platform configurations.

所属分类

依赖

依赖清单 5 项

源码仓库

使用此插件的项目