Advertisement
Guest User

product_model.dart

a guest
Nov 14th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.12 KB | None | 0 0
  1. import 'dart:convert';
  2.  
  3. class Product {
  4.   int id;
  5.   String nama;
  6.   int harga;
  7.   String jenis;
  8.   String photo;
  9.   String idUser;
  10.  
  11.   Product(
  12.       {this.id = 0,
  13.       this.nama,
  14.       this.harga,
  15.       this.jenis,
  16.       this.photo,
  17.       this.idUser});
  18.  
  19.   factory Product.fromJson(Map<String, dynamic> map) {
  20.     return Product(
  21.         id: map['id'],
  22.         nama: map['nama'],
  23.         harga: map['harga'],
  24.         jenis: map['jenis'],
  25.         photo: map['photo'],
  26.         idUser: map['id_user']);
  27.   }
  28.  
  29.   Map<String, dynamic> toJson() {
  30.     return {
  31.       'id': id,
  32.       'nama': nama,
  33.       'harga': harga,
  34.       'jenis': jenis,
  35.       'photo': photo,
  36.       'idUserr': idUser
  37.     };
  38.   }
  39.  
  40.   String toString() {
  41.     return 'Product{id: $id, name: $nama, harga: $harga, jenis: $jenis, photo: $photo, idUser: $idUser}';
  42.   }
  43. }
  44.  
  45. List<Product> productFromJson(String jsonData) {
  46.   final data = json.decode(jsonData);
  47.   return List<Product>.from(data.map((item) => {Product.fromJson(item)}));
  48. }
  49.  
  50. String productToJson(Product data) {
  51.   final jsonData = data.toJson();
  52.   return json.encode(jsonData);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement