Skip to content

Instantly share code, notes, and snippets.

@AbhishekDoshi26
Created February 10, 2022 10:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AbhishekDoshi26/7be9d99d7dfd6d8015c74a334b3b94bb to your computer and use it in GitHub Desktop.
Save AbhishekDoshi26/7be9d99d7dfd6d8015c74a334b3b94bb to your computer and use it in GitHub Desktop.
import 'dart:convert';
List<UserModel> userModelFromJson(String str) =>
List<UserModel>.from(json.decode(str).map((x) => UserModel.fromJson(x)));
String userModelToJson(List<UserModel> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class UserModel {
UserModel({
required this.id,
required this.name,
required this.username,
required this.email,
required this.address,
required this.phone,
required this.website,
required this.company,
});
int id;
String name;
String username;
String email;
Address address;
String phone;
String website;
Company company;
factory UserModel.fromJson(Map<String, dynamic> json) => UserModel(
id: json["id"],
name: json["name"],
username: json["username"],
email: json["email"],
address: Address.fromJson(json["address"]),
phone: json["phone"],
website: json["website"],
company: Company.fromJson(json["company"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"username": username,
"email": email,
"address": address.toJson(),
"phone": phone,
"website": website,
"company": company.toJson(),
};
}
class Address {
Address({
required this.street,
required this.suite,
required this.city,
required this.zipcode,
required this.geo,
});
String street;
String suite;
String city;
String zipcode;
Geo geo;
factory Address.fromJson(Map<String, dynamic> json) => Address(
street: json["street"],
suite: json["suite"],
city: json["city"],
zipcode: json["zipcode"],
geo: Geo.fromJson(json["geo"]),
);
Map<String, dynamic> toJson() => {
"street": street,
"suite": suite,
"city": city,
"zipcode": zipcode,
"geo": geo.toJson(),
};
}
class Geo {
Geo({
required this.lat,
required this.lng,
});
String lat;
String lng;
factory Geo.fromJson(Map<String, dynamic> json) => Geo(
lat: json["lat"],
lng: json["lng"],
);
Map<String, dynamic> toJson() => {
"lat": lat,
"lng": lng,
};
}
class Company {
Company({
required this.name,
required this.catchPhrase,
required this.bs,
});
String name;
String catchPhrase;
String bs;
factory Company.fromJson(Map<String, dynamic> json) => Company(
name: json["name"],
catchPhrase: json["catchPhrase"],
bs: json["bs"],
);
Map<String, dynamic> toJson() => {
"name": name,
"catchPhrase": catchPhrase,
"bs": bs,
};
}
@R12-cookies
Copy link

Brilliant, thanks a lot.

@iamjohnvesly
Copy link

Thank you

@Elien-Vos
Copy link

Thank you!

@mukolweke
Copy link

mukolweke commented Jan 26, 2023

love the tool used to generate this file. https://app.quicktype.io/

@samstickkz
Copy link

Thanks man

@Nuelik
Copy link

Nuelik commented Aug 2, 2023

Thanks 👍

@Zarlyk97
Copy link

Thank you

@Ihimbru-K
Copy link

Great

@mgdigi
Copy link

mgdigi commented Dec 8, 2023

Merciiii

@fawad-code
Copy link

cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment