What does jsonDecode() return in Dart?
jsonDecode() converts a JSON string to Dart objects: a Map for objects {} or a List for arrays []. You then convert to typed models.
Parse JSON API responses into Dart model classes
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Almost every Flutter app communicates with an API that returns JSON data. JSON arrives as plain text that Dart converts into a Map of strings and dynamic values. Working directly with these maps is fragile because you get no type checking, no autocomplete, and any typo in a key name becomes a runtime crash instead of a compile-time error. The solution is to create typed Dart model classes with fromJson and toJson methods that handle the conversion in one place. This gives you full IDE support, compile-time safety, and a clean separation between raw API data and the rest of your app.
1// ── JSON response from API ──2// {3// "id": 1,4// "name": "Alice Johnson",5// "email": "alice@example.com",6// "score": 1500,7// "isActive": true8// }910// ── Model Class ──11class User {12final int id;13final String name;14final String email;15final int score;16final bool isActive;1718const User({19required this.id,20required this.name,21required this.email,22required this.score,23required this.isActive,24});2526// ── fromJson constructor ──27factory User.fromJson(Map<String, dynamic> json) {28return User(29id: json['id'] as int,30name: json['name'] as String,31email: json['email'] as String,32score: json['score'] as int? ?? 0, // handle optional/null fields33isActive: json['isActive'] as bool? ?? false,34);35}3637// ── toJson method ──38Map<String, dynamic> toJson() {39return {40'id': id,41'name': name,42'email': email,43'score': score,44'isActive': isActive,45};46}4748@override49String toString() => 'User($name, $email)';50}
1// ── Parsing in your service ──2import 'dart:convert';3import 'package:http/http.dart' as http;45Future<User> fetchUser(int id) async {6final response = await http.get(Uri.parse('https://api.example.com/users/$id'));78if (response.statusCode == 200) {9final json = jsonDecode(response.body); // Map<String, dynamic>10return User.fromJson(json); // convert to typed model11} else {12throw Exception('Failed to load user: ${response.statusCode}');13}14}1516// ── Parsing a list of items ──17Future<List<User>> fetchUsers() async {18final response = await http.get(Uri.parse('https://api.example.com/users'));1920if (response.statusCode == 200) {21final List<dynamic> jsonList = jsonDecode(response.body);22return jsonList.map((json) => User.fromJson(json)).toList();23}24throw Exception('Failed to load users');25}2627// ── Using in Flutter ──28FutureBuilder<List<User>>(29future: fetchUsers(),30builder: (context, snapshot) {31if (snapshot.hasData) {32return ListView.builder(33itemCount: snapshot.data!.length,34itemBuilder: (_, i) => ListTile(35title: Text(snapshot.data![i].name),36subtitle: Text(snapshot.data![i].email),37),38);39}40return const CircularProgressIndicator();41},42)
For complex models with many fields or nested objects, use the json_serializable package to auto-generate fromJson and toJson methods instead of writing them by hand. Add the @JsonSerializable() annotation to your class and run dart run build_runner build to generate the code.
JSON contracts evolve, so parsing must be defensive:
Defensive parsing reduces runtime crashes from backend contract drift.
Parse JSON API responses into Dart model classes. Dart is the language behind Flutter, and clear Dart code makes your Flutter UI easier to maintain.
Connect each concept to real app features you can build.
Prefer readable names, explicit intent, and async/await over deeply nested callbacks.
When Dart basics are weak, state bugs, null crashes, and async confusion increase quickly.
jsonDecode() converts a JSON string to Dart objects: a Map for objects {} or a List for arrays []. You then convert to typed models.
factory User.fromJson(Map<String, dynamic> json) is a factory constructor that creates a User from raw JSON data.
jsonDecode returns a List<dynamic>. Using .map() to call fromJson on each element and .toList() converts it to List<User>.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue