๐ŸŒ Lesson 11: Fetch Live Data

โฑ 35-45 min ๐Ÿ“Š Advanced ๐ŸŒ Real APIs ๐Ÿ”ฅ Integration 2026 ๐Ÿ”— Official Source
๐ŸŒ

Bringing the Internet into Your App

Your CLI can do amazing things locally. Now let's connect it to the world! Live data from the web turns your app from a calculator into a real tool.

๐Ÿ’ป Your App
โ†’ HTTP Request โ†’
๐ŸŒ Internet
โ† JSON Response โ†
๐Ÿ“ฆ Data Model
๐ŸŽฏ

Today's Project: "Wikipedia Explorer CLI"

We'll build a complete Wikipedia client that fetches real data:

  • ๐Ÿ” Search Wikipedia articles
  • ๐Ÿ“„ Fetch article summaries
  • ๐Ÿ›ก๏ธ Handle network errors gracefully
  • ๐Ÿ“Š Display formatted results
  • ๐ŸŒ Respect API rate limits
Step 1

Making Your First HTTP Request

๐Ÿงช Live HTTP Request Simulator

See how an HTTP GET request works (simulated in browser):

๐Ÿ“ก Request Details:

final url = Uri.parse('https://en.wikipedia.org/...');
final response = await http.get(url);

๐Ÿ“ฅ Response:

Status: Waiting...
Headers will appear here
// Response body will appear here
Step 2

Handling HTTP Status Codes

๐Ÿšฆ Status Code Explorer

Click a status code to see how to handle it:

Click a status code to learn more

Understanding HTTP status codes is crucial for robust API clients.

Step 3

Building a Search Function

๐Ÿ” Wikipedia Search Simulator

Try searching Wikipedia (simulated results):

Enter a search term to see results

๐Ÿ“„ Search Code:

Future>> searchWikipedia(String query) async {
  final url = Uri.parse(
    'https://en.wikipedia.org/w/api.php'
    '?action=opensearch'
    '&format=json'
    '&search=${Uri.encodeComponent(query)}'
    '&limit=10'
  );
  
  final response = await http.get(url);
  // ... parse JSON array
}
Step 4

Error Handling & Resilience

๐Ÿ›ก๏ธ Network Error Simulator

See how different failures are handled:

โฐ Timeout

Request takes too long

๐Ÿ”Œ DNS Failure

Cannot resolve host

๐Ÿ’ฅ Server Error

500 Internal Server Error

๐Ÿšซ Rate Limited

429 Too Many Requests

Click a scenario to see how it's handled

๐Ÿ”„ Retry with Exponential Backoff

Future fetchWithRetry(Future Function() fn) async {
  for (int attempt = 1; attempt <= 3; attempt++) {
    try {
      return await fn().timeout(Duration(seconds: 5));
    } catch (e) {
      if (attempt == 3) rethrow;
      await Future.delayed(Duration(seconds: attempt * 2));
    }
  }
  throw Exception('Unreachable');
}
๐Ÿ†

Build: Wikipedia Explorer CLI

๐ŸŒ Wikipedia Service Class

๐Ÿ“ฆ Data Models (using pattern matching)

๐Ÿ–ฅ๏ธ CLI Application

๐ŸŽฎ Live Demo (Simulated)

$ dart run bin/cli.dart
Enter a command to see output

๐ŸŽฎ Fetch Challenge: API Master Quiz

๐ŸŒ Score: 0/5
โšก Streak: 0
โฑ Time: 0s
๐Ÿ“ถ Signal: 100%

Loading question...