Advertisement
Muhammad_Farroos

code model simpan

May 19th, 2025
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.33 KB | None | 0 0
  1. import 'dart:convert';
  2.  
  3. class ProductResponseModel {
  4.     final String? status;
  5.     final List<Product>? data;
  6.  
  7.     ProductResponseModel({
  8.         this.status,
  9.         this.data,
  10.     });
  11.  
  12.     ProductResponseModel copyWith({
  13.         String? status,
  14.         List<Product>? data,
  15.     }) =>
  16.         ProductResponseModel(
  17.             status: status ?? this.status,
  18.             data: data ?? this.data,
  19.         );
  20.  
  21.     factory ProductResponseModel.fromJson(String str) => ProductResponseModel.fromMap(json.decode(str));
  22.  
  23.     String toJson() => json.encode(toMap());
  24.  
  25.     factory ProductResponseModel.fromMap(Map<String, dynamic> json) => ProductResponseModel(
  26.         status: json["status"],
  27.         data: json["data"] == null ? [] : List<Product>.from(json["data"]!.map((x) => Product.fromMap(x))),
  28.     );
  29.  
  30.     Map<String, dynamic> toMap() => {
  31.         "status": status,
  32.         "data": data == null ? [] : List<dynamic>.from(data!.map((x) => x.toMap())),
  33.     };
  34. }
  35.  
  36. class Product {
  37.     final int? id;
  38.     final String? name;
  39.     final String? description;
  40.     final int? price;
  41.     final int? stock;
  42.     final int? categoryId;
  43.     final String? image;
  44.     final String? status;
  45.     final String? criteria;
  46.     final int? favorite;
  47.     final DateTime? createdAt;
  48.     final DateTime? updatedAt;
  49.     final Category? category;
  50.  
  51.     Product({
  52.         this.id,
  53.         this.name,
  54.         this.description,
  55.         this.price,
  56.         this.stock,
  57.         this.categoryId,
  58.         this.image,
  59.         this.status,
  60.         this.criteria,
  61.         this.favorite,
  62.         this.createdAt,
  63.         this.updatedAt,
  64.         this.category,
  65.     });
  66.  
  67.     Product copyWith({
  68.         int? id,
  69.         String? name,
  70.         String? description,
  71.         int? price,
  72.         int? stock,
  73.         int? categoryId,
  74.         String? image,
  75.         String? status,
  76.         String? criteria,
  77.         int? favorite,
  78.         DateTime? createdAt,
  79.         DateTime? updatedAt,
  80.         Category? category,
  81.     }) =>
  82.         Product(
  83.             id: id ?? this.id,
  84.             name: name ?? this.name,
  85.             description: description ?? this.description,
  86.             price: price ?? this.price,
  87.             stock: stock ?? this.stock,
  88.             categoryId: categoryId ?? this.categoryId,
  89.             image: image ?? this.image,
  90.             status: status ?? this.status,
  91.             criteria: criteria ?? this.criteria,
  92.             favorite: favorite ?? this.favorite,
  93.             createdAt: createdAt ?? this.createdAt,
  94.             updatedAt: updatedAt ?? this.updatedAt,
  95.             category: category ?? this.category,
  96.         );
  97.  
  98.     factory Product.fromJson(String str) => Product.fromMap(json.decode(str));
  99.  
  100.     String toJson() => json.encode(toMap());
  101.  
  102.     factory Product.fromMap(Map<String, dynamic> json) => Product(
  103.         id: json["id"],
  104.         name: json["name"],
  105.         description: json["description"],
  106.         price: json["price"],
  107.         stock: json["stock"],
  108.         categoryId: json["category_id"],
  109.         image: json["image"],
  110.         status: json["status"],
  111.         criteria: json["criteria"],
  112.         favorite: json["favorite"],
  113.         createdAt: json["created_at"] == null ? null : DateTime.parse(json["created_at"]),
  114.         updatedAt: json["updated_at"] == null ? null : DateTime.parse(json["updated_at"]),
  115.         category: json["category"] == null ? null : Category.fromMap(json["category"]),
  116.     );
  117.  
  118.     Map<String, dynamic> toMap() => {
  119.         "id": id,
  120.         "name": name,
  121.         "description": description,
  122.         "price": price,
  123.         "stock": stock,
  124.         "category_id": categoryId,
  125.         "image": image,
  126.         "status": status,
  127.         "criteria": criteria,
  128.         "favorite": favorite,
  129.         "created_at": createdAt?.toIso8601String(),
  130.         "updated_at": updatedAt?.toIso8601String(),
  131.         "category": category?.toMap(),
  132.     };
  133. }
  134.  
  135. class Category {
  136.     final int? id;
  137.     final String? name;
  138.     final String? description;
  139.     final DateTime? createdAt;
  140.     final DateTime? updatedAt;
  141.  
  142.     Category({
  143.         this.id,
  144.         this.name,
  145.         this.description,
  146.         this.createdAt,
  147.         this.updatedAt,
  148.     });
  149.  
  150.     Category copyWith({
  151.         int? id,
  152.         String? name,
  153.         String? description,
  154.         DateTime? createdAt,
  155.         DateTime? updatedAt,
  156.     }) =>
  157.         Category(
  158.             id: id ?? this.id,
  159.             name: name ?? this.name,
  160.             description: description ?? this.description,
  161.             createdAt: createdAt ?? this.createdAt,
  162.             updatedAt: updatedAt ?? this.updatedAt,
  163.         );
  164.  
  165.     factory Category.fromJson(String str) => Category.fromMap(json.decode(str));
  166.  
  167.     String toJson() => json.encode(toMap());
  168.  
  169.     factory Category.fromMap(Map<String, dynamic> json) => Category(
  170.         id: json["id"],
  171.         name: json["name"],
  172.         description: json["description"],
  173.         createdAt: json["created_at"] == null ? null : DateTime.parse(json["created_at"]),
  174.         updatedAt: json["updated_at"] == null ? null : DateTime.parse(json["updated_at"]),
  175.     );
  176.  
  177.     Map<String, dynamic> toMap() => {
  178.         "id": id,
  179.         "name": name,
  180.         "description": description,
  181.         "created_at": createdAt?.toIso8601String(),
  182.         "updated_at": updatedAt?.toIso8601String(),
  183.     };
  184. }
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement