Thuta Learning
Flutter
AdvancedMobile DevelopmentBeginner

Fetching Data (http)

FlutterLesson 2525 min

What you'll walk away with

  • Explain the widget, state, and lifecycle behavior of Fetching Data (http)
  • Test responsive, failure, and interaction cases
  • Write accessible, testable, and performant Flutter

Explain it simply

Fetching Data (http), in plain language

Flutter apps often need to pull data from backend APIs, public APIs, or database services. You can use the http package to make GET/POST requests. On the UI side, FutureBuilder lets you display loading, success, and error states based on conditions.

Problem → Why → Solution

The problem — why this concept matters

It's easy to assume an error is a Flutter bug without checking things like internet permission, package imports, dependency versions, or the API response format. Read the console log.

A common mistake

Assuming one screen's sample output proves every rebuild, overflow, accessibility, async, and lifecycle path.

Core concept — understand how it works

Build Fetching Data (http) as a flow that separates UI, domain logic, data sources, and failure states. Handle loading, empty, success, error, retry, cancellation, and route restoration explicitly.

Mental model
  1. async request
  2. repository or service
  3. data or error UI

Explain the widget, state, and lifecycle behavior of Fetching Data (http)

Core syntax or pattern

dart
// Add the dependency to pubspec.yaml
// dependencies:
//   http: ^1.2.0

Future<String> fetchTitle() async {
  final response = await http.get(
    Uri.parse('https://jsonplaceholder.typicode.com/posts/1'),
  );

  if (response.statusCode == 200) {
    final data = jsonDecode(response.body);
    return data['title'];
  }

  throw Exception('Failed to load title');
}

FutureBuilder<String>(
  future: fetchTitle(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return const CircularProgressIndicator();
    }

    if (snapshot.hasError) {
      return Text('Error: ${snapshot.error}');
    }

    return Text(snapshot.data ?? 'No title');
  },
)

Build Fetching Data (http) as a flow that separates UI, domain logic, data sources, and failure states. Handle loading, empty, success, error, retry, cancellation, and route restoration explicitly.

Read the code step by step

  1. async request// Add the dependency to pubspec.yaml

    Flutter apps often need to pull data from backend APIs, public APIs, or database services. You can use the http package to make GET/POST requests. On the UI side, FutureBuilder lets you display loading, success, and error states based on conditions.

  2. repository or service}

    Build Fetching Data (http) as a flow that separates UI, domain logic, data sources, and failure states. Handle loading, empty, success, error, retry, cancellation, and route restoration explicitly.

  3. data or error UI)

    Run and test an original Fetching Data (http) example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

Execution flow
  1. async request
  2. repository or service
  3. data or error UI

Success

Explain the widget, state, and lifecycle behavior of Fetching Data (http)

Failure

It's easy to assume an error is a Flutter bug without checking things like internet permission, package imports, dependency versions, or the API response format. Read the console log.

Flutter apps often need to pull data from backend APIs, public APIs, or database services. You can use the http package to make GET/POST requests. On the UI side, FutureBuilder lets you display loading, success, and error states based on conditions.

dart
// Add the dependency to pubspec.yaml
// dependencies:
//   http: ^1.2.0

Future<String> fetchTitle() async {
  final response = await http.get(
    Uri.parse('https://jsonplaceholder.typicode.com/posts/1'),
  );

  if (response.statusCode == 200) {
    final data = jsonDecode(response.body);
    return data['title'];
  }

  throw Exception('Failed to load title');
}

FutureBuilder<String>(
  future: fetchTitle(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return const CircularProgressIndicator();
    }

    if (snapshot.hasError) {
      return Text('Error: ${snapshot.error}');
    }

    return Text(snapshot.data ?? 'No title');
  },
)
You should see
A loading spinner appears at first, and once the data arrives, the title text is shown. If an error occurs, an error message appears instead.

Next Steps

In the final Mini Project lesson, bring most of these Flutter concepts together to build a small app.

Real-world usage

How to apply it in a real project

Run and test an original Fetching Data (http) example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

Mini challenge

Build it yourself

Run and test an original Fetching Data (http) example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
dart
// Add the dependency to pubspec.yaml
// dependencies:
//   http: ^1.2.0

Future<String> fetchTitle() async {
  final response = await http.get(
    Uri.parse('https://jsonplaceholder.typicode.com/posts/1'),
  );

  if (response.statusCode == 200) {
    final data = jsonDecode(response.body);
    return data['title'];
  }

  throw Exception('Failed to load title');
}

FutureBuilder<String>(
  future: fetchTitle(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return const CircularProgressIndicator();
    }

    if (snapshot.hasError) {
      return Text('Error: ${snapshot.error}');
    }

    return Text(snapshot.data ?? 'No title');
  },
)

Expected: Explain the widget, state, and lifecycle behavior of Fetching Data (http)

Show hint

Run and test an original Fetching Data (http) example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

Quick quiz 1 / 3

What best describes the core idea of Fetching Data (http)?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • async request → repository or service → data or error UI
  • Build Fetching Data (http) as a flow that separates UI, domain logic, data sources, and failure states. Handle loading, empty, success, error, retry, cancellation, and route restoration explicitly.
  • It's easy to assume an error is a Flutter bug without checking things like internet permission, package imports, dependency versions, or the API response format. Read the console log.
  • Run and test an original Fetching Data (http) example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

Flutter documentation — Flutter

Easy traps

  • It's easy to assume an error is a Flutter bug without checking things like internet permission, package imports, dependency versions, or the API response format. Read the console log.
  • Assuming one screen's sample output proves every rebuild, overflow, accessibility, async, and lifecycle path.
  • Doing expensive work in build(), leaking controllers or subscriptions, or calling setState over an unnecessarily large subtree.

Hands-on Exercise

Run and test an original Fetching Data (http) example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

You'll know it worked when: A loading spinner appears at first, and once the data arrives, the title text is shown. If an error occurs, an error message appears instead.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Fetching Data (http) | Thuta Learning