Flutter
Background Tasks
Asynchronous Programming
Mobile Development
Dart

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.

Browse interview questions

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.

dart
1import 'dart:isolate';
2
3Future<int> expensiveSum(List<int> values) async {
4  return Isolate.run(() {
5    return values.fold(0, (sum, item) => sum + item);
6  });
7}

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:

dart
1import 'package:flutter/widgets.dart';
2import 'package:workmanager/workmanager.dart';
3
4const syncTask = 'syncTask';
5
6('vm:entry-point')
7void callbackDispatcher() {
8  Workmanager().executeTask((task, inputData) async {
9    if (task == syncTask) {
10      // Do lightweight background sync here.
11    }
12    return Future.value(true);
13  });
14}
15
16void main() async {
17  WidgetsFlutterBinding.ensureInitialized();
18  await Workmanager().initialize(callbackDispatcher);
19  runApp(const MyApp());
20}

Then register the work:

dart
1await Workmanager().registerOneOffTask(
2  'sync-job-1',
3  syncTask,
4);

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, or Isolate.run for CPU work while the app is open
  • use workmanager for 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:

dart
1await Workmanager().registerOneOffTask(
2  'sync-job-2',
3  syncTask,
4  inputData: {
5    'userId': '42',
6    'force': true,
7  },
8);

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 workmanager for 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.