What is a Dart Completer and when should you use it?
This tests bridging callback APIs into Dart's Future ecosystem. An answer defines Completer as a manual Future producer, explains completing with value or error from callbacks, preferring Future() when possible. A red flag is using Completer for simple async.
What's really being asked
This question probes whether you understand the boundary between Dart's Future-based async model and legacy callback-style APIs. Specifically, it checks if you know how to manually create and resolve a Future when the standard Future constructors or async/await sugar cannot bridge an external callback system.
The full answer
First, a crisp definition: Completer is a class in dart:async that acts as a manual factory for a Future. You expose its future property to callers immediately, then resolve it later via complete(value) or completeError(error, stackTrace). Second, the canonical use case: wrapping a callback-based API, such as a native platform channel, an older HTTP client, or a third-party SDK that emits results through onSuccess and onFailure handlers. Third, the critical caveat that Completer is a tool of last resort for Future creation; you should prefer Future(() => ...) or chaining then() when the async flow is already under your control. Fourth, mention of isCompleted to guard against double-completion, which can throw a StateError.
The mistakes people make
A major red flag is suggesting Completer for routine async work like network requests or file I/O where Future and async/await are cleaner. Another mistake is forgetting to handle error paths, leaving the Completer's future dangling forever if the callback signals failure. Some candidates also miss that calling complete twice throws, so they do not mention defensive checks or careful lifecycle management.
What usually comes next
The interviewer may ask how you would cancel or timeout a Completer-based operation. They might also probe thread safety or isolate boundaries, since Completers should not be sent across isolates if they hold VM-unsendable state. Another common follow-up is asking how to convert a Stream into a Future, which can sometimes be done with Completer but is better handled with Stream.first or similar.
A concrete example
Imagine wrapping an Android Location Services API that calls onLocationChanged with a location object and onError with a message. You create a Completer of Location inside a method, return completer.future immediately, and inside the callbacks invoke completer.complete(location) or completer.completeError(Exception(msg)). You also check completer.isCompleted before calling complete to avoid crashes from duplicate callbacks.
Interview question
In which scenario is using a Dart Completer the most appropriate choice?
- a.Wrapping a third-party SDK that delivers results via onSuccess and onFailure callbacks, exposing a Future to callers immediately.Correct
- b.Chaining multiple network requests where each step already returns a Future and uses async/await.
- c.Handling simple file I/O operations where the dart:io methods already return Futures.
- d.Creating a periodic timer that emits values at regular intervals inside a Flutter widget.
Why? this is the answer
Completer is designed to manually produce a Future when bridging external callback-based APIs, such as third-party SDKs with onSuccess/onFailure handlers. Using it for routine async work like chaining existing Futures or simple file I/O is an anti-pattern because async/await and standard Future constructors are cleaner and safer.
Just read this? Test yourself on what you have been reading.
Read the original → api.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 dart — each one lists the topics its interview covers.
See open roles