搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案