How do you run a task in the background in flutter?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Flutter, "background task" can mean two very different things: work you move off the UI isolate while the app is active, or work the operating system is allowed to run after the app is backgrounded. The correct implementation depends on which of those problems you are actually solving.
Use an Isolate for Heavy Work While the App Is Alive
If the app is open and you only want to avoid blocking the UI, use a background isolate. This is good for JSON parsing, image processing, or other CPU-heavy work.
This keeps the main isolate responsive. It does not, however, give you a persistent scheduled background job after the app is suspended or terminated.
Use a Scheduler for Real Background Execution
For periodic sync, uploads, or deferred work when the app is not foregrounded, use a platform-backed solution. In Flutter today, workmanager is a common wrapper around Android WorkManager and iOS background task APIs.
Basic setup:
Then register the work:
This is the right pattern for deferrable jobs that the system can run later under platform rules.
Understand the Platform Limits
Background execution on mobile is constrained by battery, privacy, and operating-system policy. Android and iOS both limit when and how background code runs.
Important consequences:
- you do not control the exact execution time for scheduled background work
- iOS is more restrictive than Android
- long-running continuous work usually needs a native foreground service on Android or a very specific allowed background mode on iOS
If your requirement is "run every minute forever," Flutter alone is not the real problem. The operating system is intentionally preventing that pattern for most apps.
Choosing the Right Tool
Use these rules of thumb:
- use
Future,compute, orIsolate.runfor CPU work while the app is open - use
workmanagerfor deferrable scheduled sync or maintenance - use native platform APIs for special cases such as audio, navigation, or location tracking
Trying to solve all of these with one generic plugin usually leads to confusion, because they are different execution models.
Passing Data to the Background Task
Small configuration values can be passed as input data:
Inside the callback, read inputData and keep the task self-contained. Background workers should not depend on UI state or an already-mounted widget tree.
Common Pitfalls
The biggest mistake is calling an async function and assuming it is now a "background task." Future and async do not move work off the main isolate by themselves. If the task is CPU-bound, the UI can still jank badly.
Another common mistake is assuming a Flutter plugin can bypass iOS or Android scheduling rules. It cannot. A scheduled background task is always subject to platform limits, battery optimization, network constraints, and app lifecycle state.
Finally, keep background handlers small and robust. They may run without a visible UI, under tight time budgets, and sometimes in a separate isolate. Heavy network logic, large dependency graphs, or assumptions about in-memory app state make these jobs much more fragile.
Summary
- Use isolates to keep heavy work off the UI while the app is active.
- Use a scheduler such as
workmanagerfor deferrable background jobs. - Do not expect exact timing or unlimited execution after the app is backgrounded.
- Treat Android and iOS background execution as platform-governed, not app-governed.
- Keep background task code small, isolated, and independent of widget state.
Related reading
- How do you structure sequential AWS service calls within lambda given all the calls are asynchronous?
- How do you synchronise projects to GitHub with Android Studio?
- How do you use AsParallel with the async and await keywords?
- How do you use asynchronous ORMs without huge callback chains?
- How do you save/store objects in SharedPreferences on Android?
- How do you set EditText to only accept numeric values in Android?
- How do you work with Non-sendable types in swift?
- How does a single thread handle asynchronous code in JavaScript
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.