FlutterIntermediate 2 min read

Show loading, data and failure deliberately

Use FutureBuilder with a stable future and make every visible asynchronous state understandable.

On this page

An asynchronous screen has more than a successful response. The user also sees waiting, an empty result or a failure. Design these states before wiring up the request.

Keep the future stable

This example uses a simulated response so you can focus on the UI. The future is created once for the state object, not on every build.

main.dart
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: TipPage()));
class TipPage extends StatefulWidget {
  const TipPage({super.key});
  @override
  State<TipPage> createState() => _TipPageState();
}
class _TipPageState extends State<TipPage> {
  late final Future<String> tip = Future.delayed(
    const Duration(seconds: 1), () => 'Change one thing, then observe.');
  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(title: const Text('A useful tip')),
    body: Center(child: FutureBuilder<String>(future: tip,
      builder: (context, snapshot) {
        if (snapshot.hasError) return const Text('Could not load the tip.');
        if (snapshot.connectionState != ConnectionState.done) {
          return const CircularProgressIndicator();
        }
        return Text(snapshot.data ?? 'No tip available.');
      })),
  );
}

Why not create it inside build?

If building the widget creates another request, an unrelated rebuild can restart the work. Store the future and replace it intentionally when the user retries or inputs change.

Make retry meaningful

A retry control should create a fresh future, show a loading state and avoid allowing a storm of duplicate requests. Log diagnostic details separately from the short message shown to the user.

Read the snapshots as visible states

Before completion, the future represents pending work and the builder shows a loading indicator. After completion, the screen checks whether an error occurred before treating the result as usable data. A successful empty value is a different outcome from a failed request.

The future is stored for the State object because build may run again while the request is still relevant. Creating another future in build could start new work just because a parent rebuilt or the theme changed.

For retry, replace the stored future deliberately and render the new waiting state. For search, also prevent an older request from replacing the result of a newer query. Review Dart futures for control flow and HTTP boundaries for transport-specific failure handling.

Try it yourself

Make the simulated future fail. Add a retry action and allow the second attempt to succeed. A reader should be able to distinguish loading, failure and success without reading the code.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

FutureBuilder (Widget of the Week) · Flutter · official channel

Preview image only. YouTube loads when you press Play.

A conceptual companion to loading, error and success UI. Use the lesson for the current source and verification steps. Watch on YouTube

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.