Async, Futures & Streams Questions with Answers

15 practice questions on Async, Futures & Streams (Flutter & Dart) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Easy

What does a Future<int> represent in Dart?

  1. An int that is computed on a separate isolate automatically
  2. An int (or an error) that arrives at some later time
  3. A list of int values that arrive one after another over time
  4. A synchronous int value that is cached after its first read

Answer: B. An int (or an error) that arrives at some later time

Explanation

A Future is a single asynchronous result: it eventually completes with a value or with an error.

Step-by-step solution

  1. A Future is one asynchronous result.
  2. Future<int> will give an int or an error later.

Why the other options are wrong

  • A. A Future does not imply another isolate.
  • C. A list over time is a Stream.
  • D. It is not synchronous.

Tip: Future = one later; Stream = many.

Question 2Easy

What is the output order?

void main() {
  Future(() => print('B'));
  print('A');
}
  1. B then A
  2. Only B
  3. A then B
  4. Only A

Answer: C. A then B

Explanation

Future(...) schedules its callback on the event queue. The synchronous print('A') runs first, then the event loop runs the scheduled callback.

Step-by-step solution

  1. print('A') executes immediately and outputs A.
  2. Future(() => print('B')) schedules 'B' to run later on the event queue.
  3. The event loop runs after all synchronous code, so 'B' is printed next.
  4. Thus, output order is A then B.
  5. Final answer: A then B.

Why the other options are wrong

  • A. B would come first only if the future ran before the synchronous print, which it never does.
  • B. B still prints, since the Future runs later on the event queue; nothing cancels it.
  • D. B does print; a Future callback runs after the synchronous code, so it is not skipped.

Tip: Always execute synchronous code before asynchronous callbacks.

Question 3Easy

Where can you use the await keyword in Dart?

  1. Inside a function body marked async (or async*)
  2. Anywhere, including inside synchronous functions
  3. Only inside a constructor or a factory constructor
  4. Only inside the build method of a widget class

Answer: A. Inside a function body marked async (or async*)

Explanation

await pauses an asynchronous function until a Future completes, so it is only allowed in async or async* functions.

Step-by-step solution

  1. await pauses until a Future completes.
  2. It needs an async context.
  3. So it is allowed only in async or async* functions.

Why the other options are wrong

  • B. Synchronous functions cannot await.
  • C. Constructors cannot be async.
  • D. build is synchronous.

Tip: await needs async.

Question 4Easy

An async function computes and returns an int. What should its declared return type be?

  1. int
  2. Stream<int>
  3. FutureOr<void>
  4. Future<int>

Answer: D. Future<int>

Explanation

An async function always wraps its result in a Future, so returning an int means the declared type is Future<int>.

Step-by-step solution

  1. An async function always returns a Future.
  2. The value it produces is an int.
  3. So the declared return type is Future<int>, which is option D.

Why the other options are wrong

  • A. Int alone would not be async; the function must return a Future.
  • B. Stream<int> is for a sequence of values, not one result.
  • C. FutureOr<void> is not the type for an async function that returns an int.

Tip: An async function that returns T is declared as Future<T>.

Question 5Medium

What does Future.wait([f1, f2, f3]) produce?

  1. A list of results that is returned synchronously to the caller
  2. A Future that completes with the value of whichever finishes first
  3. A Future of a list of results, in input order, once all complete
  4. A Stream that emits each result as soon as its Future completes

Answer: C. A Future of a list of results, in input order, once all complete

Explanation

Future.wait runs the futures concurrently and completes with all values in the same order as the input. If any future fails, the combined Future completes with an error.

Step-by-step solution

  1. Future.wait runs all futures at the same time.
  2. It waits for all futures to finish before proceeding.
  3. Results are collected in the same order as input list.
  4. All results are packaged into a single Future object.
  5. The final Future returns a list of results in input order.

Why the other options are wrong

  • A. Future.wait cannot return results synchronously; the caller must await the Future it returns.
  • B. Future.wait waits for all futures, not just the first one; for the first one, Future.any is used.
  • D. Future.wait returns a single Future, not a Stream that emits each result as it completes.

Tip: Remember: Future.wait waits for all, returns list in input order.

Question 6Medium

When should a Flutter app move work to another isolate, for example with Isolate.run or compute?

  1. For any await, because async code always runs on the UI thread
  2. Heavy CPU work, like parsing a huge JSON, that blocks frames
  3. For setState calls, so that the UI rebuild happens in parallel
  4. For every network call, since HTTP requests otherwise block the UI

Answer: B. Heavy CPU work, like parsing a huge JSON, that blocks frames

Explanation

Async I/O does not block the UI isolate, but long synchronous computation does. Isolates have their own memory and event loop, so CPU-heavy work there keeps frames smooth.

Step-by-step solution

  1. Isolates run separately from the main UI thread.
  2. Heavy CPU tasks block the UI if done there.
  3. Move such work to isolates for smooth performance.
  4. Parsing huge JSON is CPU-heavy and blocks frames.
  5. So, use Isolate.run or compute for heavy CPU work.

Why the other options are wrong

  • A. Async code does not always run on the UI thread; await frees the thread, so most awaits need no isolate.
  • C. setState only schedules a rebuild on the UI thread; moving it to an isolate does not help.
  • D. Most HTTP calls wait without blocking the UI; isolates are for heavy CPU work, not every network call.

Tip: Move heavy computations to isolates, not async code.

Question 7Medium

Why is creating the Future inside build, as in FutureBuilder(future: fetchUser(), ...), usually a bug?

  1. build can run many times, so each rebuild starts a new request
  2. Futures created during build are never allowed to complete
  3. FutureBuilder only accepts Futures that have already completed
  4. Calling an async function inside build is a compile error

Answer: A. build can run many times, so each rebuild starts a new request

Explanation

build may be called on every frame or parent rebuild. Create the Future once (for example in initState) and pass the stored Future to FutureBuilder.

Step-by-step solution

  1. Build runs many times, so each call starts a new fetchUser() request.
  2. New requests happen every frame or parent rebuild.
  3. This causes redundant and unnecessary network calls.
  4. Each Future is separate, leading to multiple ongoing requests.
  5. Final answer: build can run many times, so each rebuild starts a new request.

Why the other options are wrong

  • B. Futures created in build are allowed to complete; the problem is that they restart on each rebuild.
  • C. FutureBuilder accepts Futures that are still pending; it shows its builder while waiting.
  • D. Calling an async function in build is valid Dart and not a compile error; the issue is repeated calls.

Tip: Move async code out of build to avoid redundant calls.

Question 8Medium

You call listen twice on the same single-subscription Stream (for example, one from a file read). What happens?

  1. The second listener silently replaces the first one
  2. Events are split between the two listeners in turns
  3. Both listeners receive every event, like a broadcast
  4. The second listen throws a StateError (Bad state)

Answer: D. The second listen throws a StateError (Bad state)

Explanation

A single-subscription stream allows only one listener in its lifetime. Use asBroadcastStream() or a broadcast StreamController for multiple listeners.

Step-by-step solution

  1. Calling listen twice on a single-subscription Stream throws an error.
  2. The error is StateError with message 'Bad state'.
  3. This happens because the stream can only have one listener at a time.
  4. Multiple listeners require a broadcast stream or controller.
  5. So, the second listen throws a StateError (Bad state).

Why the other options are wrong

  • A. Single-subscription streams do not replace listeners; a second listen is rejected, not a silent swap.
  • B. Events are never shared in turns; a single-subscription stream allows one listener, so there is no splitting.
  • C. Only broadcast streams deliver to many listeners; a single-subscription stream does not behave like one.

Tip: Never call listen twice on a single-subscription Stream.

Question 9Hard

What happens when this runs?

Future<void> f() async {
  throw 'x';
}

void main() {
  try {
    f();
  } catch (e) {
    print('caught');
  }
  print('done');
}
  1. It prints caught only, and done is skipped
  2. It throws 'x' synchronously before printing anything
  3. It prints done, then reports an unhandled error 'x'
  4. It prints caught and then done

Answer: C. It prints done, then reports an unhandled error 'x'

Explanation

An exception inside an async function completes the returned Future with an error instead of throwing synchronously. Since f() is not awaited, the try/catch never sees it and the error is unhandled.

Step-by-step solution

  1. The async function f returns a Future immediately without blocking.
  2. The try block calls f but does not await the result.
  3. The catch block is skipped because no synchronous error occurred.
  4. The code continues and prints done to the console.
  5. The Future from f completes with an error later.
  6. It prints done, then reports an unhandled error 'x'.

Why the other options are wrong

  • A. The catch never sees the error, because f() is not awaited; the caught message is not printed.
  • B. The async function returns a Future, so nothing is thrown synchronously and the try block completes.
  • D. The catch does not run, because the error stays inside the Future rather than reaching the try block.

Tip: Always await async calls inside try-catch to catch their errors.

Question 10Hard

A top-level int counter = 0; is incremented 100 times inside a closure passed to Isolate.run. After awaiting it, main prints counter. What is printed?

  1. Nothing, because globals cannot be used in an isolate
  2. 0, because each isolate has its own copy of global state
  3. A random value, because of a data race between isolates
  4. 100, because isolates share all top-level variables

Answer: B. 0, because each isolate has its own copy of global state

Explanation

Isolates do not share memory; each one has its own heap and its own top-level variables. Results must be passed back as messages, such as the closure's return value.

Step-by-step solution

  1. Isolate.run creates a new isolate with its own memory.
  2. The closure runs in the new isolate and increments counter to 100.
  3. The main isolate does not share the updated value of counter.
  4. Main prints the original global value of counter, which is 0.
  5. Isolates cannot directly modify shared top-level variables.
  6. Final answer: 0.

Why the other options are wrong

  • A. Globals can be used in an isolate; each isolate just gets its own copy, so nothing is blocked.
  • C. No data race occurs, since isolates do not share memory; the main isolate's counter stays at 0.
  • D. Isolates do not share top-level variables; each isolate has its own copy, so the 100 stays in the child.

Tip: Never assume isolates share variables; always pass results explicitly.

Practise all 15 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Flutter & Dart topics · All topics