Sub topic 2
Why Hand-Written Parsing Does Not Scale
This chapter assumes you know Dart classes, constructors, null safety, maps, lists, and the basic shape of a Flutter project. You should also understand that a REST API can return JSON text that must be decoded before Dart can use it.
The manual approach
Dart's dart:convert library decodes a JSON object into a Map<String, dynamic>. A JSON array becomes a List<dynamic>. You can then write a factory constructor that reads each value:
import 'dart:convert';
class User {
const User({
required this.id,
required this.name,
this.email,
});
factory User.fromJson(Map<String, dynamic> json) {
return User(
id: json['id'] as int,
name: json['name'] as String,
email: json['email'] as String?,
);
}
final int id;
final String name;
final String? email;
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
'email': email,
};
}
}
void main() {
const responseBody = '{"id":1,"name":"Ada","email":"[email protected]"}';
final json = jsonDecode(responseBody) as Map<String, dynamic>;
final user = User.fromJson(json);
print(user.name);
}This example is clear and works well for a small model. The problem is repetition. Every model needs code for both directions:
- JSON to a Dart object with
fromJson. - A Dart object to JSON with
toJson. - Nested objects and lists.
- Renamed keys such as
created_at. - Missing values, defaults, and type checks.
- Enums, dates, and other special types.
As the model changes, several methods must change together. Missing one field can produce incomplete requests or silently incorrect state.
Where the risk grows
Suppose an API changes email from an optional string to a required value. A manual parser might still accept a missing value if it casts the field as nullable. The application then discovers the problem much later, perhaps while displaying a screen or sending another request.
A nested model adds more places for mistakes:
class Order {
const Order({
required this.id,
required this.items,
});
factory Order.fromJson(Map<String, dynamic> json) {
final rawItems = json['items'] as List<dynamic>;
return Order(
id: json['id'] as int,
items: rawItems
.map((item) => LineItem.fromJson(item as Map<String, dynamic>))
.toList(),
);
}
final int id;
final List<LineItem> items;
}
class LineItem {
const LineItem({required this.sku});
factory LineItem.fromJson(Map<String, dynamic> json) {
return LineItem(sku: json['sku'] as String);
}
final String sku;
}The parser must remember every cast and every conversion. More models multiply this work.
What code generation changes
json_serializable generates repetitive serialization code from annotations placed on your model. You keep the model definition and its configuration in one place. build_runner creates the generated Dart file.
Generated code does not remove the need to understand the JSON contract. It reduces duplicated code and makes configuration visible through annotations. You still need to decide how strict parsing should be and how malformed data should be handled.
| Concern | Manual parsing | Generated parsing |
|---|---|---|
| Small model | Simple to start | Requires package setup |
| Repeated fields | More duplicated code | Generated consistently |
| Nested models | Manual casts and mappings | Generated conversions |
| API changes | Easy to miss a location | Regenerate and test |
Exercise
Why can a manual parser become fragile when a model contains nested lists?
Show answer
Each nested value needs its own cast and conversion, so a schema change or missed mapping can produce incomplete or incorrectly typed objects.