Why are stale search requests problematic and how do you cancel them?
This tests race conditions and resource waste in async UI. Strong answers note stale requests waste bandwidth and overwrite newer results; cancel the previous call with a CancelToken before issuing the next.
What's really being asked
This question probes whether you understand the race condition created by overlapping asynchronous network calls in a user interface. Specifically, it checks if you recognize that network latency is not deterministic, so request two may return before request one, and that unconstrained requests waste client and server resources.
The full answer
First, the candidate should articulate why the original request is harmful: it consumes unnecessary bandwidth, drains battery via radio usage, and can cause a stale result to clobber the newer correct result if it returns later. Second, the candidate should outline a cancellation strategy, such as using a CancelToken in Dio. The pattern is to store one CancelToken for the current search operation; when a new keystroke arrives, call cancel() on the existing token to abort the in-flight request, then instantiate a fresh token for the new request. Third, a senior candidate should mention that cancellation should be paired with debouncing so that rapid keystrokes do not spawn requests at all, but debouncing alone is insufficient because even a 300 millisecond debounce window can still produce overlapping requests on slow networks.
The mistakes people make
A weak answer suggests debouncing is enough and ignores cancellation entirely. Another red flag is claiming that setting a new state variable will simply ignore the old response; this prevents the UI from updating but still wastes the network and battery cost. Some candidates also propose global mutexes or sequential queues that block the new request until the old one finishes, which artificially slows the user experience instead of prioritizing the latest input.
What usually comes next
The interviewer may ask how you would handle this if the API does not support cancellation, in which case you might track request sequence numbers and drop responses that arrive out of order. They might also ask how cancellation interacts with error handling, specifically catching the DioException with type DioExceptionType.cancel to avoid logging an aborted request as a failure. Another follow-up is how this scales to a repository pattern or BLoC architecture, where the cancellation logic should live in the data layer rather than the widget.
A concrete example
Imagine a search field on a product catalog. The user types flutte, triggering a search at the 300 millisecond debounce mark. The request is dispatched with CancelToken A. While the request is in flight, the user types r, making the term flutter. Before dispatching the new request with CancelToken B, the view model calls cancel() on Token A. Dio tears down the underlying HTTP connection. The new request returns in 80 milliseconds and the UI shows results for flutter. If Token A had not been cancelled, it might have returned two seconds later and overwritten the correct results.
Interview question
When using a 300 ms debounce on a search field, why must you still cancel in-flight requests before dispatching new ones?
- a.Without cancellation, rapid keystrokes accumulate in a queue and block subsequent requests until each prior search completes.
- b.Debouncing lowers request frequency, but slow networks can still produce overlapping calls that waste bandwidth and overwrite newer results.Correct
- c.State variables can suppress stale UI updates, so cancellation is only needed to reduce server load.
- d.Reassigning the CancelToken to a new request automatically aborts the previous HTTP connection in Dio.
Why? this is the answer
Debouncing alone cannot prevent overlapping requests on slow networks, which waste bandwidth and allow stale responses to overwrite newer UI state. Distractor C echoes the common misconception that state guards alone are sufficient, yet they still waste network resources and battery without true cancellation.
Just read this? Test yourself on what you have been reading.
Read the original → pub.dev
- #flutter
- #dart
- #networking
- #async
- #cancellation
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
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