为什么我会出现这个错误;未处理的异常:类型';双倍';不是类型为';int'&引用;在Flatter中解析JSOn数据时?
我想在我的应用程序的页面视图中将youtube缩略图列表显示为横幅列表。 我使用youtube上的视频解析了Json数据,但得到了一个异常“未处理的异常:类型'double'不是类型'int'的子类型” 我将把我所做的一切记下来,并作一个小小的解释为什么我会出现这个错误;未处理的异常:类型';双倍';不是类型为';int'&引用;在Flatter中解析JSOn数据时?,json,flutter,dart,Json,Flutter,Dart,我想在我的应用程序的页面视图中将youtube缩略图列表显示为横幅列表。 我使用youtube上的视频解析了Json数据,但得到了一个异常“未处理的异常:类型'double'不是类型'int'的子类型” 我将把我所做的一切记下来,并作一个小小的解释 { "home_banners": [ { "movie_id": "youtube_LnNcjFpILQA", "title": &
{
"home_banners": [
{
"movie_id": "youtube_LnNcjFpILQA",
"title": "Andhadhun",
"tags": [
"Crime",
"Thriller"
],
"genres": [
"5e10bfe2923885a21499490b"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BZWZhMjhhZmYtOTIzOC00MGYzLWI1OGYtM2ZkN2IxNTI4ZWI3XkEyXkFqcGdeQXVyNDAzNDk0MTQ@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/LnNcjFpILQA/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:00:32.613849"
},
{
"movie_id": "youtube_jzKGrOGnERc",
"title": "Madaari",
"tags": [
"Adventure",
"Drama",
"Thriller"
],
"genres": [
"5e10bfdf923885a2149948ff"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BZDFjODM0NDAtNGJkZC00NmRiLWI3NmYtZDFkYmRhYmE4Zjk3XkEyXkFqcGdeQXVyNTIwODMzNjc@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/jzKGrOGnERc/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:00:33.318952"
},
{
"movie_id": "youtube_886EXj5MXl0",
"title": "Rakht Charitra 1",
"tags": [
"Action",
"Biography",
"Crime",
"Drama",
"Thriller"
],
"genres": [
"5e10bfdc923885a2149948f7",
"5e10bfe2923885a21499490b"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BNDU1NjAxNTMxNF5BMl5BanBnXkFtZTgwNDUzMjk4NTE@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/886EXj5MXl0/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:00:32.000245"
},
{
"movie_id": "youtube_lz1N4r7SGrY",
"title": "Satyamev Jayate",
"tags": [
"Action",
"Drama",
"Thriller"
],
"genres": [
"5e10bfdc923885a2149948f7"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BYzYxNGI3MmYtMWQyMi00NmViLWE0ZWQtNWM5ZTY3NTIxNzU0XkEyXkFqcGdeQXVyODE5NzE3OTE@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/lz1N4r7SGrY/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:00:33.404778"
},
{
"movie_id": "youtube_Pwidjiomr9w",
"title": "Mohejo Daaro",
"tags": [
"Action",
"Adventure",
"Drama",
"History"
],
"genres": [
"5e10bfdf923885a2149948ff"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BMzBhNTk0MzUtZTc4Yy00NDk3LWE1ZTItYjFmODQ4NTczMGVkL2ltYWdlL2ltYWdlXkEyXkFqcGdeQXVyNDkxMzY0Mjk@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/Pwidjiomr9w/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:00:32.666504"
}
],
"series": [
{
"series_id": "youtube_PLcvCQa6KHYRkR01iUhrTy-Ng0sDy5G3bk",
"title": "A.I.SHA My Virtual Girlfriend",
"tags": [
"Thrillers"
],
"genres": [
"5e10bfe1923885a214994908"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BNDM0MWRiNWMtMDUxMi00M2Y5LThkYzUtMjkwYmE0ZWE5YTZhXkEyXkFqcGdeQXVyNjY2Mzk3Mzg@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/X3flTlvbfBE/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:25:03.378572"
},
{
"series_id": "youtube_PL4x7Of-X4XhiK_gmCcJF8xfrlQN_46nmj",
"title": "What the Folks",
"tags": [
"Comedy"
],
"genres": [
"5e10bfde923885a2149948fc"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BZWM4YWYxYjEtZDU1Yi00NjIzLTkwYTItMGQ4MThjNDA2ZDFkXkEyXkFqcGdeQXVyNDAzNDk0MTQ@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/1rQJXDGc6OY/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:25:03.163742"
},
{
"series_id": "youtube_PLxY80KSzpL2NoLLzmWHUJEHk3_J1eWJWw",
"title": "Bachelors",
"tags": [
"Comedy"
],
"genres": [
"5e10bfde923885a2149948fc"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BYTc2M2U0NDUtNjgzMC00YmI5LTlmOTktZTZlMGRkYTNiODNhXkEyXkFqcGdeQXVyNzEyNTM4MTU@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/5XuimeH5hME/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:25:03.416601"
},
{
"series_id": "youtube_PLjq6DwYksrzz_fsWIpPcf6V7p2RNAneKc",
"title": "The Age of A.I.",
"tags": [
"Documentary"
],
"genres": [
"5e10bfdf923885a2149948fe"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BMmE3Y2M4YzYtOWRhZS00OTA2LTgyZDUtYTA4MTBkNTRmNThiXkEyXkFqcGdeQXVyNjUxMjc1OTM@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/UwsrzCVZAb8/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 1.0,
"createdAt": "2020-08-27T15:25:03.385511"
},
{
"series_id": "discoveryplus_jamies-quick-and-easy-food",
"title": "Jamie's Quick and Easy Food",
"tags": [
"Food"
],
"genres": [
"5e10bfdf923885a2149948fe"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BNGI0ZjZlYTktYzk2OC00MmQwLTgwMmUtYTM2MGZkYTYwNDIyXkEyXkFqcGdeQXVyODc4MDI5MjI@.jpg"
],
"poster_link": "https://ap2-prod-images.disco-api.com/2020/03/06/afecc471-5ad6-4770-bd79-ee5928bda612.jpeg?bf=0&f=jpg&p=true&q=75&w=600",
"platform": "discoveryplus",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:25:02.745905"
},
{
"series_id": "youtube_PLZFXeozdia12APH8n9xsVNhd5qallmrz2",
"title": "The Katering Show",
"tags": [
"Comedy"
],
"genres": [
"5e10bfde923885a2149948fc"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BOTU4NjU4NDQwOV5BMl5BanBnXkFtZTgwMTIyNjk1NDE@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/J55vgxNaaBY/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 1.0,
"createdAt": "2020-08-27T15:25:03.280574"
},
{
"series_id": "youtube_PLTB0eCoUXErb7pV0Sj1hXWrxerSZX1qgh",
"title": "Kota Factory",
"tags": [
"Comedy",
"Drama"
],
"genres": [
"5e10bfde923885a2149948fc",
"5e10bfdf923885a214994900"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BNWFlNTY5MzEtNDhhMy00YTRmLWJhYjMtM2JjODkyMGRkZmUzXkEyXkFqcGdeQXVyOTAzMTc2MjA@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/JWbnEt3xuos/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:25:03.283542"
},
{
"series_id": "discoveryplus_morgan-freemans-through-the-wormhole",
"title": "Morgan Freeman's Through the Wormhole",
"tags": [
"Science"
],
"genres": [
"5e10bfdf923885a2149948fe"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BMDhkMTY2NGQtMWY4Mi00ZjhmLWIzNGYtZDJhODkwNTM5NTIzXkEyXkFqcGdeQXVyNDk3NDEzMzk@.jpg"
],
"poster_link": "https://ap2-prod-images.disco-api.com/2020/03/04/0bc1cb72-2899-4b5c-b36e-fff7e4ecba6a.jpeg?bf=0&f=jpg&p=true&q=75&w=600",
"platform": "discoveryplus",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:25:02.742655"
},
{
"series_id": "youtube_PLGzz7pyosmlJfx9ivigemSouoZR9uLT2-",
"title": "Minus One",
"tags": [
"Comedy"
],
"genres": [
"5e10bfde923885a2149948fc"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BNWU2YWIwYjAtZGEyMS00ZDk1LWFmZWMtOTU1NzMxZGZiMjhjXkEyXkFqcGdeQXVyMTc2NTE3MDM@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/iyL9-EE3ngk/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:25:03.209943"
},
{
"series_id": "youtube_PLcUQLQkgCREYaBc7S_b8Zp0HNJsciE9bA",
"title": "Mom and Co",
"tags": [
"Drama"
],
"genres": [
"5e10bfdf923885a214994900"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BYmQ1NjEyYmYtODFjZi00ZWZmLTg0NzEtNTRmNTliMjc2ZTVjXkEyXkFqcGdeQXVyMzI3MjU2MTg@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/tPQ85zH-Vr4/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:25:03.373151"
}
],
"movies": [
{
"movie_id": "youtube_Jlnr61dEk3c",
"title": "Holiday",
"tags": [
"Action",
"Thriller"
],
"genres": [
"5e10bfdc923885a2149948f7"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BMTVkMzljMzUtZmIxMS00YWNlLTlkNDMtYmU0Nzc3MjU3OGUwXkEyXkFqcGdeQXVyODMyODMxNDY@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/Jlnr61dEk3c/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:00:32.523685"
},
{
"movie_id": "youtube_HVyXEbOWrp4",
"title": "Hungama",
"tags": [
"Comedy",
"Drama",
"Romance"
],
"genres": [
"5e10bfde923885a2149948fb",
"5e10bfdf923885a2149948ff"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BNTVjMWY5MGUtNjU2MS00NzgxLWJhZWUtM2VjMDE4YWRkOWFjXkEyXkFqcGdeQXVyODAzNzAwOTU@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/HVyXEbOWrp4/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:00:32.410578"
},
{
"movie_id": "youtube_ye5HlqJtYKA",
"title": "Golmaal",
"tags": [
"Action",
"Comedy",
"Drama",
"Family"
],
"genres": [
"5e10bfde923885a2149948fb"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BYmE3YmNiM2YtM2MxMi00ZDhmLTk1ZTQtODdjOWY4YjVkYThmXkEyXkFqcGdeQXVyNDUzOTQ5MjY@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/ye5HlqJtYKA/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:00:33.788722"
},
{
"movie_id": "youtube_FX3pmM7Duvs",
"title": "Garam Masala",
"tags": [
"Comedy",
"Romance"
],
"genres": [
"5e10bfde923885a2149948fb"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BMTk4ZWVjY2EtYzc4NS00OWVjLTgwMmMtYTAxMDNkNDEwM2QyXkEyXkFqcGdeQXVyODE5NzE3OTE@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/FX3pmM7Duvs/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:00:32.389208"
},
{
"movie_id": "youtube_qdz_wO4YJFU",
"title": "Rann",
"tags": [
"Drama",
"Mystery",
"Thriller"
],
"genres": [
"5e10bfdf923885a2149948ff",
"5e10bfe1923885a214994907"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BZjZhNzg5NTAtNTBiOC00NmI2LWI1ZDMtNzBhY2Q1ZDg5MzU3XkEyXkFqcGdeQXVyMzU0ODc1MTQ@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/qdz_wO4YJFU/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:00:33.501221"
},
{
"movie_id": "youtube_Pwidjiomr9w",
"title": "Mohejo Daaro",
"tags": [
"Action",
"Adventure",
"Drama",
"History"
],
"genres": [
"5e10bfdf923885a2149948ff"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BMzBhNTk0MzUtZTc4Yy00NDk3LWE1ZTItYjFmODQ4NTczMGVkL2ltYWdlL2ltYWdlXkEyXkFqcGdeQXVyNDkxMzY0Mjk@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/Pwidjiomr9w/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:00:32.666504"
},
{
"movie_id": "youtube_886EXj5MXl0",
"title": "Rakht Charitra 1",
"tags": [
"Action",
"Biography",
"Crime",
"Drama",
"Thriller"
],
"genres": [
"5e10bfdc923885a2149948f7",
"5e10bfe2923885a21499490b"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BNDU1NjAxNTMxNF5BMl5BanBnXkFtZTgwNDUzMjk4NTE@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/886EXj5MXl0/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0,
"createdAt": "2020-08-27T15:00:32.000245"
}
],
还有其他的事情,但为了简单起见,我没有写它们
我在中生成后得到的PODO文件如下:
// To parse this JSON data, do
//
// final homePage = homePageFromJson(jsonString);
import 'dart:convert';
HomePage homePageFromJson(String str) => HomePage.fromJson(json.decode(str));
String homePageToJson(HomePage data) => json.encode(data.toJson());
class HomePage {
HomePage({
this.homeBanners,
this.series,
this.movies,
this.musicCategories,
this.musicPlaylists,
this.publishers,
this.liveChannels,
});
List<HomeBanner> homeBanners;
List<HomeBanner> series;
List<HomeBanner> movies;
List<Music> musicCategories;
Music musicPlaylists;
List<Publisher> publishers;
List<LiveChannel> liveChannels;
factory HomePage.fromJson(Map<String, dynamic> json) => HomePage(
homeBanners: List<HomeBanner>.from(json["home_banners"].map((x) => HomeBanner.fromJson(x))),
series: List<HomeBanner>.from(json["series"].map((x) => HomeBanner.fromJson(x))),
movies: List<HomeBanner>.from(json["movies"].map((x) => HomeBanner.fromJson(x))),
musicCategories: List<Music>.from(json["music_categories"].map((x) => Music.fromJson(x))),
musicPlaylists: Music.fromJson(json["music_playlists"]),
publishers: List<Publisher>.from(json["publishers"].map((x) => Publisher.fromJson(x))),
liveChannels: List<LiveChannel>.from(json["live_channels"].map((x) => LiveChannel.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"home_banners": List<dynamic>.from(homeBanners.map((x) => x.toJson())),
"series": List<dynamic>.from(series.map((x) => x.toJson())),
"movies": List<dynamic>.from(movies.map((x) => x.toJson())),
"music_categories": List<dynamic>.from(musicCategories.map((x) => x.toJson())),
"music_playlists": musicPlaylists.toJson(),
"publishers": List<dynamic>.from(publishers.map((x) => x.toJson())),
"live_channels": List<dynamic>.from(liveChannels.map((x) => x.toJson())),
};
}
class HomeBanner {
HomeBanner({
this.movieId,
this.title,
this.tags,
this.genres,
this.thumbnail,
this.posterLink,
this.platform,
this.worldwide,
this.createdAt,
this.seriesId,
});
String movieId;
String title;
List<String> tags;
List<String> genres;
List<String> thumbnail;
String posterLink;
Platform platform;
int worldwide;
DateTime createdAt;
String seriesId;
factory HomeBanner.fromJson(Map<String, dynamic> json) => HomeBanner(
movieId: json["movie_id"] == null ? null : json["movie_id"],
title: json["title"],
tags: List<String>.from(json["tags"].map((x) => x)),
genres: List<String>.from(json["genres"].map((x) => x)),
thumbnail: List<String>.from(json["thumbnail"].map((x) => x)),
posterLink: json["poster_link"],
platform: platformValues.map[json["platform"]],
worldwide: json["WORLDWIDE"],
createdAt: DateTime.parse(json["createdAt"]),
seriesId: json["series_id"] == null ? null : json["series_id"],
);
Map<String, dynamic> toJson() => {
"movie_id": movieId == null ? null : movieId,
"title": title,
"tags": List<dynamic>.from(tags.map((x) => x)),
"genres": List<dynamic>.from(genres.map((x) => x)),
"thumbnail": List<dynamic>.from(thumbnail.map((x) => x)),
"poster_link": posterLink,
"platform": platformValues.reverse[platform],
"WORLDWIDE": worldwide,
"createdAt": createdAt.toIso8601String(),
"series_id": seriesId == null ? null : seriesId,
};
}
enum Platform { YOUTUBE, DISCOVERYPLUS }
final platformValues = EnumValues({
"discoveryplus": Platform.DISCOVERYPLUS,
"youtube": Platform.YOUTUBE
});
class LiveChannel {
LiveChannel({
this.keyId,
this.postContent,
this.publisherId,
this.publisherName,
this.publisherProfilePic,
this.publisherDesc,
this.downvotesCount,
this.upvotesCount,
});
String keyId;
PostContent postContent;
String publisherId;
String publisherName;
String publisherProfilePic;
String publisherDesc;
int downvotesCount;
int upvotesCount;
factory LiveChannel.fromJson(Map<String, dynamic> json) => LiveChannel(
keyId: json["key_id"],
postContent: PostContent.fromJson(json["post_content"]),
publisherId: json["publisher_id"],
publisherName: json["publisher_name"],
publisherProfilePic: json["publisher_profile_pic"],
publisherDesc: json["publisher_desc"],
downvotesCount: json["downvotes_count"],
upvotesCount: json["upvotes_count"],
);
Map<String, dynamic> toJson() => {
"key_id": keyId,
"post_content": postContent.toJson(),
"publisher_id": publisherId,
"publisher_name": publisherName,
"publisher_profile_pic": publisherProfilePic,
"publisher_desc": publisherDesc,
"downvotes_count": downvotesCount,
"upvotes_count": upvotesCount,
};
}
class PostContent {
PostContent({
this.shortcode,
this.platformVideoLink,
this.caption,
this.description,
});
String shortcode;
String platformVideoLink;
String caption;
String description;
factory PostContent.fromJson(Map<String, dynamic> json) => PostContent(
shortcode: json["shortcode"],
platformVideoLink: json["platform_videoLink"],
caption: json["caption"],
description: json["description"],
);
Map<String, dynamic> toJson() => {
"shortcode": shortcode,
"platform_videoLink": platformVideoLink,
"caption": caption,
"description": description,
};
}
class Music {
Music({
this.id,
this.country,
this.categoryId,
this.categoryName,
this.categoryIcons,
this.playlists,
});
dynamic id;
String country;
String categoryId;
String categoryName;
List<CategoryIcon> categoryIcons;
List<Playlist> playlists;
factory Music.fromJson(Map<String, dynamic> json) => Music(
id: json["_id"],
country: json["country"],
categoryId: json["category_id"],
categoryName: json["category_name"],
categoryIcons: List<CategoryIcon>.from(json["category_icons"].map((x) => CategoryIcon.fromJson(x))),
playlists: List<Playlist>.from(json["playlists"].map((x) => Playlist.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"_id": id,
"country": country,
"category_id": categoryId,
"category_name": categoryName,
"category_icons": List<dynamic>.from(categoryIcons.map((x) => x.toJson())),
"playlists": List<dynamic>.from(playlists.map((x) => x.toJson())),
};
}
class CategoryIcon {
CategoryIcon({
this.height,
this.url,
this.width,
});
int height;
String url;
int width;
factory CategoryIcon.fromJson(Map<String, dynamic> json) => CategoryIcon(
height: json["height"] == null ? null : json["height"],
url: json["url"],
width: json["width"] == null ? null : json["width"],
);
Map<String, dynamic> toJson() => {
"height": height == null ? null : height,
"url": url,
"width": width == null ? null : width,
};
}
class Playlist {
Playlist({
this.playlistName,
this.playlistDescription,
this.playlistUrl,
this.playlistTotalTracks,
this.playlistImages,
this.playlistFollowers,
this.playlistId,
});
String playlistName;
String playlistDescription;
String playlistUrl;
int playlistTotalTracks;
List<CategoryIcon> playlistImages;
int playlistFollowers;
String playlistId;
factory Playlist.fromJson(Map<String, dynamic> json) => Playlist(
playlistName: json["playlist_name"],
playlistDescription: json["playlist_description"],
playlistUrl: json["playlist_url"],
playlistTotalTracks: json["playlist_total_tracks"],
playlistImages: List<CategoryIcon>.from(json["playlist_images"].map((x) => CategoryIcon.fromJson(x))),
playlistFollowers: json["playlist_followers"],
playlistId: json["playlist_id"],
);
Map<String, dynamic> toJson() => {
"playlist_name": playlistName,
"playlist_description": playlistDescription,
"playlist_url": playlistUrl,
"playlist_total_tracks": playlistTotalTracks,
"playlist_images": List<dynamic>.from(playlistImages.map((x) => x.toJson())),
"playlist_followers": playlistFollowers,
"playlist_id": playlistId,
};
}
class Publisher {
Publisher({
this.platform,
this.username,
this.fullName,
this.profilePicUrl,
this.content,
this.keyId,
});
Platform platform;
String username;
String fullName;
String profilePicUrl;
Content content;
String keyId;
factory Publisher.fromJson(Map<String, dynamic> json) => Publisher(
platform: platformValues.map[json["platform"]],
username: json["username"],
fullName: json["full_name"],
profilePicUrl: json["profile_pic_url"],
content: Content.fromJson(json["content"]),
keyId: json["key_id"],
);
Map<String, dynamic> toJson() => {
"platform": platformValues.reverse[platform],
"username": username,
"full_name": fullName,
"profile_pic_url": profilePicUrl,
"content": content.toJson(),
"key_id": keyId,
};
}
class Content {
Content({
this.description,
});
String description;
factory Content.fromJson(Map<String, dynamic> json) => Content(
description: json["description"],
);
Map<String, dynamic> toJson() => {
"description": description,
};
}
class EnumValues<T> {
Map<String, T> map;
Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
if (reverseMap == null) {
reverseMap = map.map((k, v) => new MapEntry(v, k));
}
return reverseMap;
}
}
这是主文件的页面视图。我有
_buildPageView(var boxHeight) {
return Container(
color: Colors.black87,
height: boxHeight,
child: PageView.builder(
itemCount: _items.length,
controller: _pageController,
itemBuilder: (BuildContext context, int index) {
try {
HomeBanner homeBanner=_homeBannelList[index];
return FadeInImage.assetNetwork(
image:
'https://img.youtube.com/vi/${homeBanner.movieId.substring(8)}/0.jpg',
placeholder: cupertinoActivityIndicator,
fit: BoxFit.fill,
);
} catch (e) {
return CircularProgressIndicator();
}
//before return Image.network('https://img.youtube.com/vi/${videoIdOfUrlList[index]}/0.jpg',fit: BoxFit.fill,);
},
onPageChanged: (int index) {
_currentPageNotifier.value = index;
}),
);
}
相关线路为'https://img.youtube.com/vi/${homeBanner.movieId.substring(8)}/0.jpg',其中homeBanner的内容被计算出来
>>
完整的Json数据可以在这里找到“为什么我在flift中解析Json数据时出现此错误”未处理的异常:类型“double”不是类型“int”的子类型?”:
您正在使用int
:
int worldwide;
将
int-worldwide
更改为double-worldwide
worldwide
是double
而不是int
{
"movie_id": "youtube_Jlnr61dEk3c",
"title": "Holiday",
"tags": [
"Action",
"Thriller"
],
"genres": [
"5e10bfdc923885a2149948f7"
],
"thumbnail": [
"https://m.media-amazon.com/images/M/MV5BMTVkMzljMzUtZmIxMS00YWNlLTlkNDMtYmU0Nzc3MjU3OGUwXkEyXkFqcGdeQXVyODMyODMxNDY@.jpg"
],
"poster_link": "https://i.ytimg.com/vi/Jlnr61dEk3c/hqdefault.jpg",
"platform": "youtube",
"WORLDWIDE": 0.0, // it is 'double'
"createdAt": "2020-08-27T15:00:32.523685"
}
从api接收的数据是float“WORLDWIDE”:0.0,,Homebarner类将其定义为int
class HomeBanner {
HomeBanner({
this.movieId
...
});
String movieId;
****
int worldwide;
DateTime createdAt;
String seriesId;
将int转换为double是的,其他人回答将int更改为double确实有效。我在这个问题上做了越来越多的工作,并且能够成功地解析数据,实际上修改是在服务类中完成的
import 'package:flutter/material.dart';
import 'package:flutterlivetv/jsonParseforHomeScreen.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'dart_json_web_token_generator.dart';
class Services {
static const String url =
"https://livetvapi.apyhi.com/api/v2/home?pageLocation=home&countries=IN&app_version=13&"
"user_id=44edc2c905ae163f&package_id=livetv.movies.freemovies.watchtv.tvshows&os_platform=android";
static Future<List<String>> loadDataForMovieId() async {
var res = await http
.get(url, headers: {'Authorization': dartJsonWebTokenGenerator()});
if (res.statusCode == 200) {
print("response is there");
final homePage = homePageFromJson(res.body);
print("homeBannerlist is there");
//print("homeBannerlist is there");
// print (homePage.homeBanners);
HomeBanner homeBannerObject=new HomeBanner();
List<String> homeBannerObjectMovieIdList=[];
for(homeBannerObject in homePage.homeBanners) homeBannerObjectMovieIdList.add(homeBannerObject.movieId);
print("Movie ID list is there");
print (homeBannerObjectMovieIdList);
return homeBannerObjectMovieIdList;
} else {
print("no response");
return null;
}
}
}
这是一个与图像相关的代码:
FadeInImage.assetNetwork(
image:
'https://img.youtube.com/vi/${_homeBannerObjectMovieIdList[index].substring(8)}/0.jpg',
placeholder: cupertinoActivityIndicator,
fit: BoxFit.fill,
);
希望这能有所帮助。基本上,它是将每一级向下,并将内容作为list.UFFF..
height==null?空:高度
无效。它应该正好是高度。将WORLDWIDE
的数据类型从int
更改为double
是的,现在错误消失了。我得到一个错误,未处理的异常:类型“HomePage”不是我尝试的类型转换列表中类型“List”的子类型homeBannerlist=homePageFromJson(res.body)…但我真的不知道该怎么办?我想这应该是另一个问题(当然是解析json
的方式的问题)。如果答案解决了你原来的问题,你可以接受。
class HomeBanner {
HomeBanner({
this.movieId
...
});
String movieId;
****
int worldwide;
DateTime createdAt;
String seriesId;
import 'package:flutter/material.dart';
import 'package:flutterlivetv/jsonParseforHomeScreen.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'dart_json_web_token_generator.dart';
class Services {
static const String url =
"https://livetvapi.apyhi.com/api/v2/home?pageLocation=home&countries=IN&app_version=13&"
"user_id=44edc2c905ae163f&package_id=livetv.movies.freemovies.watchtv.tvshows&os_platform=android";
static Future<List<String>> loadDataForMovieId() async {
var res = await http
.get(url, headers: {'Authorization': dartJsonWebTokenGenerator()});
if (res.statusCode == 200) {
print("response is there");
final homePage = homePageFromJson(res.body);
print("homeBannerlist is there");
//print("homeBannerlist is there");
// print (homePage.homeBanners);
HomeBanner homeBannerObject=new HomeBanner();
List<String> homeBannerObjectMovieIdList=[];
for(homeBannerObject in homePage.homeBanners) homeBannerObjectMovieIdList.add(homeBannerObject.movieId);
print("Movie ID list is there");
print (homeBannerObjectMovieIdList);
return homeBannerObjectMovieIdList;
} else {
print("no response");
return null;
}
}
}
List<String> _homeBannerObjectMovieIdList = [];
@override
void initState() {
// TODO: implement initState
super.initState();
handleScroll(); // function which is responsible for updating the isScrollingDown variable whenever the user scrolls down
Services.loadDataForMovieId().then((movieIdList) {
setState(() {
_homeBannerObjectMovieIdList = movieIdList;
});
});
}
FadeInImage.assetNetwork(
image:
'https://img.youtube.com/vi/${_homeBannerObjectMovieIdList[index].substring(8)}/0.jpg',
placeholder: cupertinoActivityIndicator,
fit: BoxFit.fill,
);