input.json
order.dart
import 'package:equatable/equatable.dart'; class Order extends Equatable { final String orderId; final Customer customer; final double total; final DateTime createdAt; final List<Item> items; const Order({ required this.orderId, required this.customer, required this.total, required this.createdAt, required this.items, }); factory Order.fromJson(Map<String, dynamic> json) => Order( orderId: json["order_id"] as String, customer: Customer.fromJson(json["customer"] as Map<String, dynamic>), total: (json["total"] as num).toDouble(), createdAt: DateTime.parse(json["created_at"] as String), items: (json["items"] as List<dynamic>).map((e) => Item.fromJson(e as Map<String, dynamic>)).toList(), ); Map<String, dynamic> toJson() => { "order_id": orderId, "customer": customer.toJson(), "total": total, "created_at": createdAt.toIso8601String(), "items": items.map((e) => e.toJson()).toList(), }; @override List<Object?> get props => [orderId, customer, total, createdAt, items]; } class Customer extends Equatable { final String firstName; final String email; final bool isVerified; const Customer({ required this.firstName, required this.email, required this.isVerified, }); factory Customer.fromJson(Map<String, dynamic> json) => Customer( firstName: json["first_name"] as String, email: json["email"] as String, isVerified: json["is_verified"] as bool, ); Map<String, dynamic> toJson() => { "first_name": firstName, "email": email, "is_verified": isVerified, }; @override List<Object?> get props => [firstName, email, isVerified]; } class Item extends Equatable { final String sku; final int quantity; final double price; final String? note; const Item({ required this.sku, required this.quantity, required this.price, this.note, }); factory Item.fromJson(Map<String, dynamic> json) => Item( sku: json["sku"] as String, quantity: json["quantity"] as int, price: (json["price"] as num).toDouble(), note: json["note"] == null ? null : json["note"] as String, ); Map<String, dynamic> toJson() => { "sku": sku, "quantity": quantity, "price": price, "note": note, }; @override List<Object?> get props => [sku, quantity, price, note]; }
Convert JSON to Equatable
Paste JSON to generate immutable classes that extend Equatable. Add the package to your project, then copy the generated Dart file; no build_runner step is required.
Output behavior
Every generated class has final fields, a const constructor, fromJson and toJson methods, and a props getter containing its fields. JSON keys are preserved in serialization even when the Dart field name is converted to camelCase.
Project setup
flutter pub add equatable
Before using it
Equatable compares the values exposed by props. If your application mutates a list or nested value after constructing a model, use immutable data or review equality behavior for that field.