Turn JSON into Dart classes with a fromJson factory and a toJson method. The code is plain Dart with no code-generation packages, so there is no build_runner step.
It fits Flutter apps that pass raw Map<String, dynamic> responses from http or dio into widgets and want proper model classes instead, or small projects that need working serialization before adopting json_serializable.
Type name (for example Store).lib/models/; no imports are needed.Store.fromJson(jsonDecode(response.body) as Map<String, dynamic>) and send them with jsonEncode(store.toJson()).{
"storeId": 77,
"storeName": "Mapo Kimbap",
"isOpen": true,
"rating": 4.7,
"menuItems": [
{ "name": "Tuna Kimbap", "price": 4500, "soldOut": false },
{ "name": "Ramyeon", "price": 5000, "soldOut": true, "spicy": 2 }
]
}class Store {
final int storeId;
final String storeName;
final bool isOpen;
final double rating;
final List<MenuItem> menuItems;
Store({
required this.storeId,
required this.storeName,
required this.isOpen,
required this.rating,
required this.menuItems,
});
factory Store.fromJson(Map<String, dynamic> json) => Store(
storeId: json['storeId'] as int,
storeName: json['storeName'] as String,
isOpen: json['isOpen'] as bool,
rating: (json['rating'] as num).toDouble(),
menuItems: (json['menuItems'] as List<dynamic>).map((e) => MenuItem.fromJson(e as Map<String, dynamic>)).toList(),
);
Map<String, dynamic> toJson() => {
'storeId': storeId,
'storeName': storeName,
'isOpen': isOpen,
'rating': rating,
'menuItems': menuItems.map((e) => e.toJson()).toList(),
};
}
class MenuItem {
final String name;
final int price;
final bool soldOut;
final int? spicy;
MenuItem({
required this.name,
required this.price,
required this.soldOut,
this.spicy,
});
factory MenuItem.fromJson(Map<String, dynamic> json) => MenuItem(
name: json['name'] as String,
price: json['price'] as int,
soldOut: json['soldOut'] as bool,
spicy: json['spicy'] as int?,
);
Map<String, dynamic> toJson() => {
'name': name,
'price': price,
'soldOut': soldOut,
'spicy': spicy,
};
}
Elements of the menuItems array become a MenuItem class, converted inside fromJson with .map(...).toList(). rating is read as (json['rating'] as num).toDouble() so an integer value is still safe, and spicy, present only on some menu items, is int? without required in the constructor.
final; constructors use named parameters ({required this.x}).factory X.fromJson(Map<String, dynamic> json) and Map<String, dynamic> toJson() with no package dependencies.fromJson / toJson.double fields are read via (as num).toDouble(), so a JSON integer like 4 doesn't throw a cast error.null become nullable (int?) and pass null through in fromJson.dynamic; Dart reserved words such as class or is get a trailing underscore.typedef Root = List<RootItem>;.Carrying a Map through FutureBuilder or Riverpod/Bloc state hides key typos until runtime. With generated classes you get completion and type checks on store.menuItems.first.price.
Store jsonEncode(store.toJson()) in shared_preferences or a file and restore it with fromJson at startup for a cache in a few lines.
Confirm the field list and types first; as the project grows, add @JsonSerializable() to the same fields and move to code generation.
5000.0 as the integer 5000. Dart's jsonDecode keeps 5000.0 as a double, so as int throws. Change price or coordinate fields that may carry a decimal point to double.toJson writes nullable fields even when they are null ('spicy': null). If your server rejects null keys, leave them out conditionally.String; convert them yourself with DateTime.parse(json['createdAt'] as String).copyWith, ==/hashCode or toString is generated. Consider freezed or equatable if you need them.dynamic fields hold values unchecked; replace them once you know the real type.The output is hand-written-style fromJson/toJson without packages. Take the field declarations, add @JsonSerializable() and part 'store.g.dart';, and run dart run build_runner build to switch to code generation.
type 'int' is not a subtype of type 'double'.That happens when code casts with as double and the JSON value is an integer. Generated double fields use (as num).toDouble() to avoid it; apply the same pattern to any field you edited by hand.
[ {...}, {...} ])?A top-level array yields a typedef and an element class. Call fromJson per element: (jsonDecode(body) as List<dynamic>).map((e) => RootItem.fromJson(e as Map<String, dynamic>)).toList().
The output uses sound null safety syntax (int?, required) from Dart 2.12 onward. Pre-null-safety projects need the ? and required removed to compile.