Background Execution and Services in Flutter
Think of background execution as a helper doing heavy lifting off-screen. It keeps your app's UI smooth by running tasks like data processing on a separate thread (Isolate). The footgun is that isolates don't share memory; you must pass data explicitly.
Why it exists
Mobile operating systems are strict about responsiveness. If an app's main thread is blocked for too long by a heavy task, the OS will terminate it, leading to a crash. Background execution exists to offload these long-running tasks, ensuring the user interface remains smooth and interactive at all times.
The mental model
Think of a Flutter app as a single workshop (the main thread) where all UI work happens. A background process is like opening a separate, isolated workshop (an Isolate) to handle a specific, heavy job. This second workshop has its own tools and materials (memory) and can't directly access the main one. To exchange information, workers must send messages through a designated communication channel (a Port).
How it works
Dart is single-threaded, but it achieves concurrency using Isolates. Each Isolate has its own memory heap and event loop, preventing race conditions by design. For simple, one-off background tasks, Flutter provides a helper function called compute(). You give it a function and some data, and it runs that function in a new Isolate, returning the result. For tasks that need to run even when the app is in the background or closed (like checking for new messages), you must use platform-specific APIs, typically through plugins like workmanager or flutter_background_service.
When to use it
Use background execution for any task that could take more than a few milliseconds and cause the UI to stutter. Common use cases include: first, fetching and parsing large JSON data from a network; second, processing images or videos; third, performing complex mathematical calculations; and fourth, syncing large amounts of data with a local database.
When not to use it
Avoid using Isolates for very short tasks. The overhead of creating a new Isolate and passing messages can be more costly than simply performing the quick operation on the main thread. Critically, you must never update the UI from a background Isolate; all UI rendering must happen on the main thread after receiving the results.
One canonical example
A news app needs to download and parse a large JSON file containing articles. Instead of freezing the UI, it calls compute(), passing it the parsing function and the raw JSON string. A new Isolate is spawned, which decodes the JSON into Dart objects. Once complete, it sends the list of article objects back to the main Isolate, which then safely updates the UI to display the headlines.
Interview question
Which statement accurately describes how Dart Isolates manage memory and communication?
- a.Data can be directly accessed and modified across different Isolates without any special handling.
- b.Each Isolate possesses its own memory heap and communicates with others by sending messages.Correct
- c.Isolates execute tasks sequentially on the main thread, ensuring thread safety without separate memory.
- d.Isolates share a common memory space, requiring explicit locking mechanisms for data integrity.
Why? this is the answer
The card states that "Each Isolate has its own memory heap and event loop" and that "To exchange information, workers must send messages through a designated communication channel (a Port)." This means Isolates do not share memory directly, making option D incorrect as it describes a shared-memory model.
Just read this? Test yourself on what you have been reading.
Read the original → docs.flutter.dev
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.
See open roles