Use the http package (or dio) to talk to REST APIs. Dart uses Future + async/await for async work.
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<List<Product>> fetchProducts() async {
final res = await http.get(Uri.parse('https://api.example.com/products'));
if (res.statusCode == 200) {
final List data = jsonDecode(res.body);
return data.map((j) => Product.fromJson(j)).toList();
}
throw Exception('Failed to load');
}
Show it with FutureBuilder
FutureBuilder<List<Product>>(
future: fetchProducts(),
builder: (context, snap) {
if (snap.connectionState != ConnectionState.done) return CircularProgressIndicator();
if (snap.hasError) return Text('Error');
return ListView(children: snap.data!.map((p) => Text(p.title)).toList());
},
)
Common mistake: Add the INTERNET permission on Android and always handle non-200 status codes and exceptions.
Summary
Fetch data with http/dio using async/await and render it with FutureBuilder, handling loading and error states.