IWorkManager class abstract interface

Testable interface for NativeWorkManager.

Inject this into services/repositories that need to schedule tasks. In production code, use NativeWorkManagerClient (wraps the real plugin). In tests, use FakeWorkManager (in-memory test double).

Migration

Before (untestable static calls):

class SyncService {
  Future<void> syncNow() async {
    await NativeWorkManager.enqueue(
      taskId: 'sync',
      trigger: TaskTrigger.oneTime(),
      worker: NativeWorker.httpSync(url: 'https://api.example.com/sync'),
    );
  }
}

After (injectable, testable):

class SyncService {
  const SyncService(this._workManager);
  final IWorkManager _workManager;

  Future<void> syncNow() async {
    await _workManager.enqueue(
      taskId: 'sync',
      trigger: TaskTrigger.oneTime(),
      worker: NativeWorker.httpSync(url: 'https://api.example.com/sync'),
    );
  }
}

// Production:
final service = SyncService(NativeWorkManagerClient());

// Test:
final fake = FakeWorkManager();
final service = SyncService(fake);
await service.syncNow();
expect(fake.enqueued.length, 1);
expect(fake.enqueued.first.taskId, 'sync');
Implementers

Properties

events Stream<TaskEvent>
Task completion and lifecycle events.
no setter
hashCode int
The hash code for this object.
no setterinherited
progress Stream<TaskProgress>
Task progress updates (downloads, uploads, chains).
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

allTasks() Future<List<TaskRecord>>
Returns all task records from the persistent store.
beginWith(TaskRequest task) TaskChainBuilder
Begin a task chain starting with task.
cancel({required String taskId}) Future<void>
Cancel a specific task by ID.
cancelAll() Future<void>
Cancel all pending and running tasks.
cancelByTag({required String tag}) Future<void>
Cancel all tasks with the given tag.
dispose() → void
Release resources held by this instance.
enqueue({required String taskId, required TaskTrigger trigger, required Worker worker, Constraints constraints, ExistingTaskPolicy existingPolicy, String? tag}) Future<TaskHandler>
Schedule a single background task.
enqueueAll(List<EnqueueRequest> requests) Future<List<TaskHandler>>
Schedule multiple tasks in one call.
enqueueGraph(TaskGraph graph) Future<GraphExecution>
Schedule a TaskGraph (directed acyclic graph) of background tasks.
getAllTags() Future<List<String>>
Returns all active tags.
getRunningProgress() Future<Map<String, TaskProgress>>
Get the current progress of all running tasks.
getTaskRecord({required String taskId}) Future<TaskRecord?>
Returns the detailed record of a task, or null if not found.
getTasksByTag({required String tag}) Future<List<String>>
Returns all task IDs associated with tag.
getTaskStatus({required String taskId}) Future<TaskStatus?>
Returns the current status of a task, or null if not found.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pause({required String taskId}) Future<void>
Pause a running task (best-effort; effective for download workers).
resume({required String taskId}) Future<void>
Resume a previously paused task.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited