Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.54 KB | None | 0 0
  1. // To parse this JSON data, do
  2. //
  3. //     final userModel = userModelFromJson(jsonString);
  4.  
  5. import 'dart:convert';
  6.  
  7. List<UserModel> userModelFromJson(String str) => new List<UserModel>.from(json.decode(str).map((x) => UserModel.fromJson(x)));
  8.  
  9. String userModelToJson(List<UserModel> data) => json.encode(new List<dynamic>.from(data.map((x) => x.toJson())));
  10.  
  11. class UserModel {
  12.   int id;
  13.   String name;
  14.   String username;
  15.   String email;
  16.   Address address;
  17.   String phone;
  18.   String website;
  19.   Company company;
  20.  
  21.   UserModel({
  22.     this.id,
  23.     this.name,
  24.     this.username,
  25.     this.email,
  26.     this.address,
  27.     this.phone,
  28.     this.website,
  29.     this.company,
  30.   });
  31.  
  32.   factory UserModel.fromJson(Map<String, dynamic> json) => new UserModel(
  33.     id: json["id"],
  34.     name: json["name"],
  35.     username: json["username"],
  36.     email: json["email"],
  37.     address: Address.fromJson(json["address"]),
  38.     phone: json["phone"],
  39.     website: json["website"],
  40.     company: Company.fromJson(json["company"]),
  41.   );
  42.  
  43.   Map<String, dynamic> toJson() => {
  44.     "id": id,
  45.     "name": name,
  46.     "username": username,
  47.     "email": email,
  48.     "address": address.toJson(),
  49.     "phone": phone,
  50.     "website": website,
  51.     "company": company.toJson(),
  52.   };
  53. }
  54.  
  55. class Address {
  56.   String street;
  57.   String suite;
  58.   String city;
  59.   String zipcode;
  60.   Geo geo;
  61.  
  62.   Address({
  63.     this.street,
  64.     this.suite,
  65.     this.city,
  66.     this.zipcode,
  67.     this.geo,
  68.   });
  69.  
  70.   factory Address.fromJson(Map<String, dynamic> json) => new Address(
  71.     street: json["street"],
  72.     suite: json["suite"],
  73.     city: json["city"],
  74.     zipcode: json["zipcode"],
  75.     geo: Geo.fromJson(json["geo"]),
  76.   );
  77.  
  78.   Map<String, dynamic> toJson() => {
  79.     "street": street,
  80.     "suite": suite,
  81.     "city": city,
  82.     "zipcode": zipcode,
  83.     "geo": geo.toJson(),
  84.   };
  85. }
  86.  
  87. class Geo {
  88.   String lat;
  89.   String lng;
  90.  
  91.   Geo({
  92.     this.lat,
  93.     this.lng,
  94.   });
  95.  
  96.   factory Geo.fromJson(Map<String, dynamic> json) => new Geo(
  97.     lat: json["lat"],
  98.     lng: json["lng"],
  99.   );
  100.  
  101.   Map<String, dynamic> toJson() => {
  102.     "lat": lat,
  103.     "lng": lng,
  104.   };
  105. }
  106.  
  107. class Company {
  108.   String name;
  109.   String catchPhrase;
  110.   String bs;
  111.  
  112.   Company({
  113.     this.name,
  114.     this.catchPhrase,
  115.     this.bs,
  116.   });
  117.  
  118.   factory Company.fromJson(Map<String, dynamic> json) => new Company(
  119.     name: json["name"],
  120.     catchPhrase: json["catchPhrase"],
  121.     bs: json["bs"],
  122.   );
  123.  
  124.   Map<String, dynamic> toJson() => {
  125.     "name": name,
  126.     "catchPhrase": catchPhrase,
  127.     "bs": bs,
  128.   };
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement