Skip to content
tezvyn:

How do you handle CPU-bound tasks without freezing the Flutter UI?

Source: docs.flutter.devMediumHow cards are made

Tests whether you know async/await yields for I/O but cannot parallelize CPU work. A strong answer defines Isolates as isolated heaps with message-passing, contrasts them with threads, and shows how compute() wraps Isolate.spawn.

What's really being asked

The interviewer wants to know if you understand the difference between concurrency and parallelism in Dart. Async and await handle I/O bound work by yielding control back to the event loop, but they do not create new threads. CPU intensive tasks like image decoding, heavy JSON parsing, or mathematical computations will block the UI isolate if run directly. You must demonstrate knowledge of Isolates as Dart's unit of parallelism and know that compute simplifies their use.

The full answer

First, clarify that Dart code runs in an isolate with a single event loop and memory heap. Async await merely pauses function execution until an I/O future completes, allowing other events to process, but all code still runs on the same thread. Second, define an Isolate as an independent worker with its own memory heap and event loop. Isolates do not share mutable state; they communicate strictly by passing messages through ports, which makes them safer than traditional threads but requires serialization overhead. Third, explain that spawning an isolate manually involves ReceivePort, SendPort, and message handling boilerplate. Fourth, describe compute as a high level utility function that wraps Isolate.spawn. It accepts a top level or static function and its argument, runs it in a new isolate, and returns a Future containing the result while automatically managing ports, message passing, and error propagation.

The mistakes people make

A red flag is suggesting Future.delayed or a Timer to unblock the UI; these merely schedule work later on the same thread. Another mistake is claiming that async await automatically moves work to a background thread. Some candidates suggest running CPU work inside a Stream or StreamTransformer, which does not parallelize execution. Confusing isolates with web workers is acceptable in concept but you must know they are Dart specific constructs. Also, stating that compute uses a thread pool is incorrect; it spawns a new isolate per call.

Likely follow ups

The interviewer may ask when to use an isolate directly instead of compute. You should answer that manual isolate management is needed for long lived workers, bidirectional communication, or when you need to spawn multiple isolates and reuse them. They might ask about the performance cost: isolate spawning takes milliseconds and message passing requires copying data, so isolates suit heavy batch work rather than trivial calculations. Another follow up is how this differs on the web, where Dart isolates compile to web workers with similar message passing semantics but different constraints.

A concrete example

Suppose you receive a one megabyte JSON string from a network request and must parse it into a Dart model. Parsing is CPU intensive. Using jsonDecode on the main isolate will drop frames. Instead, you call compute with a static parse function and the raw string as the argument. Compute spawns an isolate, passes the string via message port, runs jsonDecode in the new isolate, serializes the resulting map back to the main isolate, and completes the Future with the parsed data, all without blocking the UI thread.

Interview question

Why does an async function containing heavy JSON parsing still cause Flutter UI jank, and what should you use instead?

  • a.The event loop blocks all timers during parsing; use Future.delayed to let the UI breathe first.
  • b.Async functions automatically move work to a background thread, but you should use a Stream to emit results incrementally.
  • c.Dart isolates share memory like threads, so spawning one requires a mutex to synchronize access.
  • d.Async/await only yields for I/O and runs on the same isolate; use compute() to parse in a new isolate.Correct
Why?

Async/await yields control during I/O but still runs CPU work on the same isolate, so heavy parsing blocks the UI; compute() spawns a new isolate to parallelize it. The distractor claiming async moves work to a background thread reflects a common misconception, since Dart isolates are the only true parallelism mechanism.

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.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles