FGWorkManager
FGWorkManager is a lightweight, easy-to-use foreground job manager for Flutter. Unlike other background job managers, it does not require complex platform-specific configurations. It ensures that jobs are cached and restored even after app restarts, allowing for a seamless job execution process.
π Key Benefits
- No platform-specific setup β Works out-of-the-box without additional Android/iOS configurations.
- Jobs persist after app restarts β Jobs are cached and restored when the app reopens.
- Two behaviors for past-due jobs after a restart:
- Execute β The job runs once the app is opened.
- Ignore β The job is discarded.
π Installation
Run the following command to add FGWorkManager to your project:
flutter pub add foreground_work_manager
Import the package:
import 'package:foreground_work_manager/foreground_work_manager.dart';
π Usage Example
await FGWorkManager.init();
await FGWorkManager.openQueue(
queueId: "testQueue",
process: (job) async {
print("Job: ${job.id} | Executed at: ${DateTime.now().toIso8601String()} | Scheduled at: ${job.time.toIso8601String()}");
},
);
await FGWorkManager.addJob(
"testQueue",
Job(
id: 'j_1',
time: DateTime.now().add(Duration(seconds: 3)),
data: {},
),
);
await FGWorkManager.addJob(
"testQueue",
Job(
id: 'j_2',
time: DateTime.now().add(Duration(seconds: 3)),
data: {},
),
);
// Remove a job before execution
await Future.delayed(Duration(seconds: 2), () async {
await FGWorkManager.removeJob(queueId: "testQueue", jobId: 'j_1');
});
// Clear queue after some time
await Future.delayed(Duration(minutes: 2), () async {
await FGWorkManager.clearQueue("testQueue");
});
await FGWorkManager.removeQueue("testQueue");
β‘ How It Works
- Jobs are added to queues, which execute tasks at their scheduled time.
- If a job is not executed before an app restart, it will follow one of two behaviors:
- Execute β The job runs when the app is reopened.
- Ignore β The job is discarded.
- Jobs are stored in a cache and automatically restored after an app restart.
π When to Use FGWorkManager?
β
When you need task execution while the app is running (not in the background).
β
When you donβt want to configure platform-specific settings.
β
When you need persistent job execution across app restarts.
β
When jobs should execute in order using a queue system.
β Need Background Execution?
If you need jobs to run even when the app is closed, consider using Workmanager, which supports native background execution.
π― Get Started Now!
FGWorkManager is an efficient solution for managing job execution without complex setups. Try it today and simplify your Flutter task management!