Skip to content
tezvyn:
Menu

Difference between Future<void> and void from an async function

Source: dart.devEasyHow cards are made

Summary

Knowledge that async always produces a Future.

Key points

Future<void> lets callers await and catch errors; void hides the Future, preventing await and leaving exceptions uncaught.

Watch out for

Believing void async returns are awaitable.

What's really being asked

This question checks if you understand that the async keyword in Dart implicitly wraps the function body in a Future before returning to the caller. It also tests whether you know that the static return type, Future<void> versus void, determines what the caller is allowed to do with that Future, especially around awaiting completion and handling errors.

The full answer

First, state that an async function always returns a Future at runtime, even if the declared return type is void. Second, explain that Future<void> gives the caller a handle to the Future, so they can await it to know when the operation finishes and wrap it in try/catch to handle exceptions. Third, explain that void hides the Future from the caller; the work still runs asynchronously, but the caller has no way to await completion and no way to catch errors thrown inside the function, which usually become uncaught async exceptions. Fourth, mention that returning void from an async function is generally an anti-pattern when the caller needs to sequence work or handle failures.

The mistakes people make

Saying that a void async function runs synchronously. Claiming that the caller can still await a void async function by ignoring the type system. Asserting that exceptions in a void async function will propagate to the caller automatically; they do not, because the caller has no Future to attach an error handler to. Confusing Future<void> with void and thinking the difference is only stylistic.

What usually comes next

When would you intentionally use void instead of Future<void>? The interviewer might ask about fire-and-forget scenarios and how to safely handle errors in them. They might also ask how this interacts with Flutter widget lifecycles, for example awaiting an async cleanup method in dispose. Another follow-up is how zone error handlers or runZonedGuarded interact with uncaught async errors from void async functions.

A concrete example

Consider a function that writes to a local cache. If you declare it as Future<void> writeCache() async { await file.writeAsString(data); }, the caller can write await writeCache() and catch any IOException. If you declare it as void writeCache() async { await file.writeAsString(data); }, the caller cannot await it. If the write fails, the resulting exception has no awaiting Future and no try/catch block attached, so it becomes an uncaught async error that may crash the app or only be caught by a zone handler.

Interview question

What is the practical consequence of declaring an async Dart function as void instead of Future<void>?

  • a.The caller gets no Future handle, so they cannot await completion or catch async errors.Correct
  • b.The function body executes synchronously, blocking the caller until it completes.
  • c.Exceptions thrown inside automatically propagate to the caller's surrounding try/catch block.
  • d.The caller can still await the function at runtime by ignoring the declared return type.
Why?

A void return type hides the implicit Future from the caller, which prevents awaiting completion and catching errors with try/catch. Option C is tempting but wrong because without a Future handle, exceptions become uncaught async errors instead of propagating to the caller.

Just read this? Test yourself on what you have been reading.

Read the original → dart.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 dart — each one lists the topics its interview covers.

See open roles