Step 1 / 5
Start with a model
A plain immutable class gives the rest of your app typed data instead of raw maps.
Call a REST API, turn JSON into typed Dart objects, and show loading and error states with FutureBuilder.
1class Post {2 const Post({required this.id, required this.title});3 final int id;4 final String title;56 factory Post.fromJson(Map<String, dynamic> json) =>7 Post(id: json['id'] as int, title: json['title'] as String);8}910Future<List<Post>> fetchPosts() async {11 final res = await http.get(12 Uri.parse('https://jsonplaceholder.typicode.com/posts'),13 );14 if (res.statusCode != 200) {15 throw Exception('Request failed: ${res.statusCode}');16 }17 final list = jsonDecode(res.body) as List<dynamic>;18 return list.map((e) => Post.fromJson(e as Map<String, dynamic>)).toList();19}
Press “Fetch posts”
jsonDecode(res.body) as List↓ .map(Post.fromJson)Post(id: 1, title: 'sunt aut…')The JSON map becomes a typed Post via Post.fromJson — the rest of the app never touches raw maps.
A plain immutable class gives the rest of your app typed data instead of raw maps.