๐ Lesson 11: Fetch Live Data
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%