i get a json response from this link https://itunes.apple.com/in/rss/topalbums/limit=1/json can anyone make a model class where i can so a http get request and use the snapshot data in future builder . I tried quicktype but the model class throwing errors like null is not a subtype of string
CodePudding user response:
Yes. It's always a good practice to convert the JSON in Models and use it in the code.
class AppleModel {
Feed? feed;
AppleModel({this.feed});
AppleModel.fromJson(Map<String, dynamic> json) {
feed = json['feed'] != null ? new Feed.fromJson(json['feed']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.feed != null) {
data['feed'] = this.feed!.toJson();
}
return data;
}
}
class Feed {
Author? author;
Entry? entry;
Name? updated;
Name? rights;
Name? title;
Name? icon;
List<Link>? link;
Name? id;
Feed(
{this.author,
this.entry,
this.updated,
this.rights,
this.title,
this.icon,
this.link,
this.id});
Feed.fromJson(Map<String, dynamic> json) {
author =
json['author'] != null ? new Author.fromJson(json['author']) : null;
entry = json['entry'] != null ? new Entry.fromJson(json['entry']) : null;
updated =
json['updated'] != null ? new Name.fromJson(json['updated']) : null;
rights = json['rights'] != null ? new Name.fromJson(json['rights']) : null;
title = json['title'] != null ? new Name.fromJson(json['title']) : null;
icon = json['icon'] != null ? new Name.fromJson(json['icon']) : null;
if (json['link'] != null) {
link = <Link>[];
json['link'].forEach((v) {
link!.add(new Link.fromJson(v));
});
}
id = json['id'] != null ? new Name.fromJson(json['id']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.author != null) {
data['author'] = this.author!.toJson();
}
if (this.entry != null) {
data['entry'] = this.entry!.toJson();
}
if (this.updated != null) {
data['updated'] = this.updated!.toJson();
}
if (this.rights != null) {
data['rights'] = this.rights!.toJson();
}
if (this.title != null) {
data['title'] = this.title!.toJson();
}
if (this.icon != null) {
data['icon'] = this.icon!.toJson();
}
if (this.link != null) {
data['link'] = this.link!.map((v) => v.toJson()).toList();
}
if (this.id != null) {
data['id'] = this.id!.toJson();
}
return data;
}
}
class Author {
Name? name;
Name? uri;
Author({this.name, this.uri});
Author.fromJson(Map<String, dynamic> json) {
name = json['name'] != null ? new Name.fromJson(json['name']) : null;
uri = json['uri'] != null ? new Name.fromJson(json['uri']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.name != null) {
data['name'] = this.name!.toJson();
}
if (this.uri != null) {
data['uri'] = this.uri!.toJson();
}
return data;
}
}
class Name {
String? label;
Name({this.label});
Name.fromJson(Map<String, dynamic> json) {
label = json['label'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['label'] = this.label;
return data;
}
}
class Entry {
Name? imName;
List<ImImage>? imImage;
Name? imItemCount;
ImImage? imPrice;
ImContentType? imContentType;
Name? rights;
Name? title;
ImContentType? link;
Name? id;
ImImage? imArtist;
ImContentType? category;
ImImage? imReleaseDate;
Entry(
{this.imName,
this.imImage,
this.imItemCount,
this.imPrice,
this.imContentType,
this.rights,
this.title,
this.link,
this.id,
this.imArtist,
this.category,
this.imReleaseDate});
Entry.fromJson(Map<String, dynamic> json) {
imName =
json['im:name'] != null ? new Name.fromJson(json['im:name']) : null;
if (json['im:image'] != null) {
imImage = <ImImage>[];
json['im:image'].forEach((v) {
imImage!.add(new ImImage.fromJson(v));
});
}
imItemCount = json['im:itemCount'] != null
? new Name.fromJson(json['im:itemCount'])
: null;
imPrice = json['im:price'] != null
? new ImImage.fromJson(json['im:price'])
: null;
imContentType = json['im:contentType'] != null
? new ImContentType.fromJson(json['im:contentType'])
: null;
rights = json['rights'] != null ? new Name.fromJson(json['rights']) : null;
title = json['title'] != null ? new Name.fromJson(json['title']) : null;
link =
json['link'] != null ? new ImContentType.fromJson(json['link']) : null;
id = json['id'] != null ? new Name.fromJson(json['id']) : null;
imArtist = json['im:artist'] != null
? new ImImage.fromJson(json['im:artist'])
: null;
category = json['category'] != null
? new ImContentType.fromJson(json['category'])
: null;
imReleaseDate = json['im:releaseDate'] != null
? new ImImage.fromJson(json['im:releaseDate'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.imName != null) {
data['im:name'] = this.imName!.toJson();
}
if (this.imImage != null) {
data['im:image'] = this.imImage!.map((v) => v.toJson()).toList();
}
if (this.imItemCount != null) {
data['im:itemCount'] = this.imItemCount!.toJson();
}
if (this.imPrice != null) {
data['im:price'] = this.imPrice!.toJson();
}
if (this.imContentType != null) {
data['im:contentType'] = this.imContentType!.toJson();
}
if (this.rights != null) {
data['rights'] = this.rights!.toJson();
}
if (this.title != null) {
data['title'] = this.title!.toJson();
}
if (this.link != null) {
data['link'] = this.link!.toJson();
}
if (this.id != null) {
data['id'] = this.id!.toJson();
}
if (this.imArtist != null) {
data['im:artist'] = this.imArtist!.toJson();
}
if (this.category != null) {
data['category'] = this.category!.toJson();
}
if (this.imReleaseDate != null) {
data['im:releaseDate'] = this.imReleaseDate!.toJson();
}
return data;
}
}
class ImImage {
String? label;
Attributes? attributes;
ImImage({this.label, this.attributes});
ImImage.fromJson(Map<String, dynamic> json) {
label = json['label'];
attributes = json['attributes'] != null
? new Attributes.fromJson(json['attributes'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['label'] = this.label;
if (this.attributes != null) {
data['attributes'] = this.attributes!.toJson();
}
return data;
}
}
class Attributes {
String? height;
Attributes({this.height});
Attributes.fromJson(Map<String, dynamic> json) {
height = json['height'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['height'] = this.height;
return data;
}
}
class Attributes {
String? amount;
String? currency;
Attributes({this.amount, this.currency});
Attributes.fromJson(Map<String, dynamic> json) {
amount = json['amount'];
currency = json['currency'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['amount'] = this.amount;
data['currency'] = this.currency;
return data;
}
}
class ImContentType {
ImContentType? imContentType;
Attributes? attributes;
ImContentType({this.imContentType, this.attributes});
ImContentType.fromJson(Map<String, dynamic> json) {
imContentType = json['im:contentType'] != null
? new ImContentType.fromJson(json['im:contentType'])
: null;
attributes = json['attributes'] != null
? new Attributes.fromJson(json['attributes'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.imContentType != null) {
data['im:contentType'] = this.imContentType!.toJson();
}
if (this.attributes != null) {
data['attributes'] = this.attributes!.toJson();
}
return data;
}
}
class ImContentType {
Attributes? attributes;
ImContentType({this.attributes});
ImContentType.fromJson(Map<String, dynamic> json) {
attributes = json['attributes'] != null
? new Attributes.fromJson(json['attributes'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.attributes != null) {
data['attributes'] = this.attributes!.toJson();
}
return data;
}
}
class Attributes {
String? term;
String? label;
Attributes({this.term, this.label});
Attributes.fromJson(Map<String, dynamic> json) {
term = json['term'];
label = json['label'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['term'] = this.term;
data['label'] = this.label;
return data;
}
}
class Attributes {
String? rel;
String? type;
String? href;
Attributes({this.rel, this.type, this.href});
Attributes.fromJson(Map<String, dynamic> json) {
rel = json['rel'];
type = json['type'];
href = json['href'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['rel'] = this.rel;
data['type'] = this.type;
data['href'] = this.href;
return data;
}
}
class Attributes {
String? imId;
Attributes({this.imId});
Attributes.fromJson(Map<String, dynamic> json) {
imId = json['im:id'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['im:id'] = this.imId;
return data;
}
}
class Attributes {
String? href;
Attributes({this.href});
Attributes.fromJson(Map<String, dynamic> json) {
href = json['href'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['href'] = this.href;
return data;
}
}
class Attributes {
String? imId;
String? term;
String? scheme;
String? label;
Attributes({this.imId, this.term, this.scheme, this.label});
Attributes.fromJson(Map<String, dynamic> json) {
imId = json['im:id'];
term = json['term'];
scheme = json['scheme'];
label = json['label'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['im:id'] = this.imId;
data['term'] = this.term;
data['scheme'] = this.scheme;
data['label'] = this.label;
return data;
}
}
Moreover, You can use this github.io to convert it into Dart Objects. Please feel free to make changes to above Attributes model as you require in some other way.
CodePudding user response:
Here:
UPDATED MODEL
class Model {
Model({
required this.feed,
});
late final Feed feed;
Model.fromJson(Map<String, dynamic> json) {
feed = Feed.fromJson(json['feed']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['feed'] = feed.toJson();
return _data;
}
}
class Feed {
Feed({
required this.author,
required this.entry,
required this.updated,
required this.rights,
required this.title,
required this.icon,
required this.link,
required this.id,
});
late final Author author;
late final Entry entry;
late final Updated updated;
late final Rights rights;
late final Title title;
late final Icon icon;
late final List<Link> link;
late final Id id;
Feed.fromJson(Map<String, dynamic> json) {
author = Author.fromJson(json['author']);
entry = Entry.fromJson(json['entry']);
updated = Updated.fromJson(json['updated']);
rights = Rights.fromJson(json['rights']);
title = Title.fromJson(json['title']);
icon = Icon.fromJson(json['icon']);
link = List.from(json['link']).map((e) => Link.fromJson(e)).toList();
id = Id.fromJson(json['id']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['author'] = author.toJson();
_data['entry'] = entry.toJson();
_data['updated'] = updated.toJson();
_data['rights'] = rights.toJson();
_data['title'] = title.toJson();
_data['icon'] = icon.toJson();
_data['link'] = link.map((e) => e.toJson()).toList();
_data['id'] = id.toJson();
return _data;
}
}
class Author {
Author({
required this.name,
required this.uri,
});
late final Name name;
late final ModelUri uri;
Author.fromJson(Map<String, dynamic> json) {
name = Name.fromJson(json['name']);
uri = ModelUri.fromJson(json['uri']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['name'] = name.toJson();
_data['uri'] = uri.toJson();
return _data;
}
}
class Name {
Name({
required this.label,
});
late final String label;
Name.fromJson(Map<String, dynamic> json) {
label = json['label'];
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
return _data;
}
}
class ModelUri {
ModelUri({
required this.label,
});
late final String label;
ModelUri.fromJson(Map<String, dynamic> json) {
label = json['label'];
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
return _data;
}
}
class Entry {
Entry({
required this.imname,
required this.imimage,
required this.imitemCount,
required this.imprice,
required this.imcontentType,
required this.rights,
required this.title,
required this.link,
required this.id,
required this.imartist,
required this.category,
required this.imreleaseDate,
});
late final Imname imname;
late final List<Imimage> imimage;
late final ImitemCount imitemCount;
late final Imprice imprice;
late final ImcontentType imcontentType;
late final Rights rights;
late final Title title;
late final Link link;
late final Id id;
late final Imartist imartist;
late final Category category;
late final ImreleaseDate? imreleaseDate;
Entry.fromJson(Map<String, dynamic> json) {
imname = Imname.fromJson(json['im:name']);
imimage =
List.from(json['im:image']).map((e) => Imimage.fromJson(e)).toList();
imitemCount = ImitemCount.fromJson(json['im:itemCount']);
imprice = Imprice.fromJson(json['im:price']);
imcontentType = ImcontentType.fromJson(json['im:contentType']);
rights = Rights.fromJson(json['rights']);
title = Title.fromJson(json['title']);
link = Link.fromJson(json['link']);
id = Id.fromJson(json['id']);
imartist = Imartist.fromJson(json['im:artist']);
category = Category.fromJson(json['category']);
imreleaseDate = json['imreleaseDate'] == null ? null : ImreleaseDate.fromJson(json['imreleaseDate']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['im:name'] = imname.toJson();
_data['im:image'] = imimage.map((e) => e.toJson()).toList();
_data['im:itemCount'] = imitemCount.toJson();
_data['im:price'] = imprice.toJson();
_data['im:contentType'] = imcontentType.toJson();
_data['rights'] = rights.toJson();
_data['title'] = title.toJson();
_data['link'] = link.toJson();
_data['id'] = id.toJson();
_data['im:artist'] = imartist.toJson();
_data['category'] = category.toJson();
_data['im:releaseDate'] = imreleaseDate?.toJson();
return _data;
}
}
class Imname {
Imname({
required this.label,
});
late final String label;
Imname.fromJson(Map<String, dynamic> json) {
label = json['label'];
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
return _data;
}
}
class Imimage {
Imimage({
required this.label,
required this.attributes,
});
late final String label;
late final Attributes attributes;
Imimage.fromJson(Map<String, dynamic> json) {
label = json['label'];
attributes = Attributes.fromJson(json['attributes']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
_data['attributes'] = attributes.toJson();
return _data;
}
}
class Attributes {
Attributes({
required this.height,
});
late final String? height;
Attributes.fromJson(Map<String, dynamic> json) {
height = json['height'];
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['height'] = height;
return _data;
}
}
class ImitemCount {
ImitemCount({
required this.label,
});
late final String label;
ImitemCount.fromJson(Map<String, dynamic> json) {
label = json['label'];
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
return _data;
}
}
class Imprice {
Imprice({
required this.label,
required this.attributes,
});
late final String label;
late final Attributes attributes;
Imprice.fromJson(Map<String, dynamic> json) {
label = json['label'];
attributes = Attributes.fromJson(json['attributes']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
_data['attributes'] = attributes.toJson();
return _data;
}
}
class ImcontentType {
ImcontentType({
required this.imcontentType,
required this.attributes,
});
late final ImcontentType? imcontentType;
late final Attributes attributes;
ImcontentType.fromJson(Map<String, dynamic> json) {
print("attribute json: $json");
imcontentType = json['im:contentType'] == null ? null : ImcontentType.fromJson(json['im:contentType']);
attributes = Attributes.fromJson(json['attributes']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['im:contentType'] = imcontentType?.toJson();
_data['attributes'] = attributes.toJson();
return _data;
}
}
class Rights {
Rights({
required this.label,
});
late final String label;
Rights.fromJson(Map<String, dynamic> json) {
label = json['label'];
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
return _data;
}
}
class Title {
Title({
required this.label,
});
late final String label;
Title.fromJson(Map<String, dynamic> json) {
label = json['label'];
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
return _data;
}
}
class Link {
Link({
required this.attributes,
});
late final Attributes attributes;
Link.fromJson(Map<String, dynamic> json) {
attributes = Attributes.fromJson(json['attributes']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['attributes'] = attributes.toJson();
return _data;
}
}
class Id {
Id({
required this.label,
required this.attributes,
});
late final String label;
late final Attributes? attributes;
Id.fromJson(Map<String, dynamic> json) {
label = json['label'];
attributes = json['attributes'] == null ? null : Attributes.fromJson(json['attributes']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
_data['attributes'] = attributes?.toJson();
return _data;
}
}
class Imartist {
Imartist({
required this.label,
required this.attributes,
});
late final String label;
late final Attributes attributes;
Imartist.fromJson(Map<String, dynamic> json) {
label = json['label'];
attributes = Attributes.fromJson(json['attributes']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
_data['attributes'] = attributes.toJson();
return _data;
}
}
class Category {
Category({
required this.attributes,
});
late final Attributes attributes;
Category.fromJson(Map<String, dynamic> json) {
attributes = Attributes.fromJson(json['attributes']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['attributes'] = attributes.toJson();
return _data;
}
}
class ImreleaseDate {
ImreleaseDate({
required this.label,
required this.attributes,
});
late final String label;
late final Attributes attributes;
ImreleaseDate.fromJson(Map<String, dynamic> json) {
label = json['label'];
attributes = Attributes.fromJson(json['attributes']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
_data['attributes'] = attributes.toJson();
return _data;
}
}
class Updated {
Updated({
required this.label,
});
late final String label;
Updated.fromJson(Map<String, dynamic> json) {
label = json['label'];
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
return _data;
}
}
class Icon {
Icon({
required this.label,
});
late final String label;
Icon.fromJson(Map<String, dynamic> json) {
label = json['label'];
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['label'] = label;
return _data;
}
}
API CALL
main() {
execute();
}
void execute() async {
Response response = await get(Uri.parse("https://itunes.apple.com/in/rss/topalbums/limit=1/json"));
print(response.body);
var x = Model.fromJson(jsonDecode(response.body));
print(x.feed.toJson());
}
