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
This mode generates final fields and const constructors. Each class extends Equatable and lists its fields in props for value equality. The output also includes fromJson and toJson methods.
Add Equatable to your Flutter project, copy the generated Dart file, and import it where the model is used. There is no build_runner step for this output.
Project setup
flutter pub add equatable