tezvyn:

Define a Dart Future, return Future<String>, and handle errors with both patterns.

AI-drafted, machine-checkedSource: dart.devbeginner

Tests Dart async primitives and error handling. A strong answer defines Future as a pending async result, writes a delayed Future<String>, consumes it with then/catchError, and mirrors with async/await try/catch.

WHAT THIS TESTS: This question tests whether you understand Dart's single-threaded async model and can move comfortably between callback-style and modern async/await patterns. Interviewers want to see that you know a Future represents a pending computation that will complete later with either a value or an error, and that you can handle both outcomes idiomatically without blocking the main thread.

A GOOD ANSWER COVERS: First, define a Future as an object representing a result that is not yet available. Second, write a function like fetchMessage that returns Future<String> and uses Future.delayed to simulate real async work such as a network call. Third, show consumption with then and catchError, chaining callbacks and returning the Future so errors propagate correctly through the chain. Fourth, show the async/await version with try/catch, emphasizing that await unwraps the completed value and try/catch captures thrown exceptions. Fifth, note that async/await is generally preferred for readability and maintainability, but then/catchError remains valid for short chains or when integrating with older code.

COMMON WRONG ANSWERS: A major red flag is treating a Future<String> as if it were a String directly, for example assigning it to a String variable without awaiting it or calling then. Another mistake is forgetting to return the Future from the function body, which breaks the caller's ability to chain or await the result. Some candidates write then/catchError but omit error typing or return statements inside callbacks, or they use async/await without try/catch and let uncaught errors propagate to the microtask queue and crash.

LIKELY FOLLOW-UPS: The interviewer may ask how you would convert a callback-based API into a Future using a Completer, how to handle multiple concurrent Futures with Future.wait, or what happens if you await a Future inside a try block and the error is caught versus uncaught. They might also ask about the event loop and microtasks, or how to handle specific error types rather than catching every exception broadly.

ONE CONCRETE EXAMPLE: Here is a concise implementation. The function fetchMessage returns Future<String> and completes after one second with hello or throws if shouldFail is true. With callbacks, you write fetchMessage.then((value) => print(value)).catchError((error) => print(error)). With async/await, you write try { final message = await fetchMessage(); print(message); } catch (e) { print(e); }. Both patterns handle the same error, but async/await reads like synchronous code and is easier to debug.

Read the original → dart.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.