Almost every app shows data from a server. In Flutter the usual pattern is to call the API with the http package, turn the JSON into a Dart class, and show the result with a FutureBuilder that handles loading and errors. This guide builds a small list screen step by step.
Step 1: add the package
flutter pub add http
Then import it where you need it:
import 'dart:convert'; import 'package:http/http.dart' as http;
On Android, apps need internet permission in release builds. Check that android/app/src/main/AndroidManifest.xml contains <uses-permission android:name="android.permission.INTERNET" />. On iOS and desktop no extra step is usually needed for HTTPS addresses.
Step 2: model the data
A class with a fromJson factory turns raw JSON into something typed and safe to use. This example uses a free test API that returns a list of posts.
class Post {
final int id;
final String title;
const Post({required this.id, required this.title});
factory Post.fromJson(Map<String, dynamic> json) {
return Post(
id: json['id'] as int,
title: json['title'] as String,
);
}
}Step 3: write the request
Future<List<Post>> fetchPosts() async {
final response = await http
.get(Uri.parse('https://jsonplaceholder.typicode.com/posts'))
.timeout(const Duration(seconds: 10));
if (response.statusCode != 200) {
throw Exception('Server returned ${response.statusCode}');
}
final list = jsonDecode(response.body) as List<dynamic>;
return list
.map((item) => Post.fromJson(item as Map<String, dynamic>))
.toList();
}Three habits worth keeping: always parse the address with Uri.parse, always set a timeout, and always check the status code before reading the body.
Step 4: show it with FutureBuilder
class PostsScreen extends StatefulWidget {
const PostsScreen({super.key});
@override
State<PostsScreen> createState() => _PostsScreenState();
}
class _PostsScreenState extends State<PostsScreen> {
late Future<List<Post>> _posts;
@override
void initState() {
super.initState();
_posts = fetchPosts(); // start once, not on every rebuild
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Posts')),
body: FutureBuilder<List<Post>>(
future: _posts,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
return Center(child: Text('Could not load: ${snapshot.error}'));
}
final posts = snapshot.data!;
return ListView.builder(
itemCount: posts.length,
itemBuilder: (context, i) => ListTile(title: Text(posts[i].title)),
);
},
),
);
}
}The most common mistake
Calling fetchPosts() directly inside build or as the future: argument creates a new request every time the widget rebuilds, which can mean a request on every keystroke or animation frame. Store the future in initState, as above, so it runs once.
Add pull-to-refresh
To let the user reload, wrap the list in a RefreshIndicator and assign a new future in setState:
RefreshIndicator(
onRefresh: () async {
setState(() => _posts = fetchPosts());
await _posts;
},
child: ListView.builder(/* ... */),
)Where to go next
- For anything beyond a simple screen, move the request into its own class so widgets do not know about URLs.
- When several screens share the data, use Provider or Riverpod. See our state management guide.
- For a larger app, our Flutter learning hub lists the rest of the series.
Covering financial markets, commodity rates, and practical technology insights for India. Researched and fact-checked in accordance with our Editorial Policy.
Related & Trending Stories
Recommended based on your reading interest
