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.
- async request
- repository or service
- data or error UI
Explain the widget, state, and lifecycle behavior of Fetching Data (http)
Core syntax or pattern
// 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
- async request
// Add the dependency to pubspec.yamlFlutter 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.
- 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.
- 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.
- async request
- repository or service
- 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.
// 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');
},
)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
// 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
Quick quiz 2 / 3
Quick quiz 3 / 3
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