JSON and Serialization in Flutter
Part 1: Manual serialization
JSON and Serialization in Flutter
Part 1: Manual serialization

credits to this link
There are two types of JSON serialization in Flutter 1] Manual serialization 2] Automated serialization using code generation
Manual Serialization
Manual serialization is used for small projects, this method is nothing but using *JSON Decoder from dart:convert *package. We just have to pass the JSON string to the jsonDecode() method. As this has no external dependencies, it is easy to start with. This will be complex if we have large data.
Suppose we want to serialize the following JSON data and use it in our application. For that, we have two ways.
{
"name": "John Smith",
"email": "john@example.com"
}
Serializing JSON inline
The JSON decode returns the Map<String, dynamic> which is error-prone as the compiler won’t be able to show you compile-time exceptions and thus your program may break in runtime. With this approach, you lose most of the statically typed language features: type safety, autocompletion, and most importantly, compile-time exceptions.
Map<String, dynamic> user = jsonDecode(jsonString);
print('Howdy, ${user['name']}!');
print('We sent the verification link to ${user['email']}.');
Serializing JSON inside model classes
For this method, we define a model class called user.dart with two methods called fromJSON(Map<String, dynamic> json) and toJSON(), thus we are now safe with the compile-time exception checking, type-safety, auto-completion.
user.dart
class User {
final String name;
final String email;
User(this.name, this.email);
User.fromJson(Map<String, dynamic> json)
: name = json['name'],
email = json['email'];
Map<String, dynamic> toJson() => {
'name': name,
'email': email,
};
}
Now you can use the following code to decode and encode JSON respectively.
JSON decode
Map<String, dynamic> userMap = jsonDecode(jsonString);
var user = User.fromJson(userMap);
print('Howdy, ${user.name}!');
print('We sent the verification link to ${user.email}.');
JSON encode
String json = jsonEncode(user);
to be continued tomorrow…….
Thank you 🙏
메타데이터
- post_id
- dac1b6c9f16
- slug
- json-and-serialization-in-flutter-dac1b6c9f16
- url
- https://medium.com/@omlondhe/json-and-serialization-in-flutter-dac1b6c9f16
- canonical_url
- https://medium.com/@omlondhe/json-and-serialization-in-flutter-dac1b6c9f16
- author_url
- https://medium.com/@omlondhe
- status
- ok
- fetched_at
- 2026-07-31 12:13:25