你应该创建一个模型如下。
// 要解析此 JSON 数据,请执行以下操作
//
// 最终结果 = resultFromJson(jsonString);
import 'dart:convert';
class Result {
String kind;
String etag;
String nextPageToken;
String regionCode;
PageInfo pageInfo;
List<Item> items;
Result({
this.kind,
this.etag,
this.nextPageToken,
this.regionCode,
this.pageInfo,
this.items,
});
factory Result.fromRawJson(String str) => Result.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Result.fromJson(Map<String, dynamic> json) => new Result(
kind: json["kind"] == null ? null : json["kind"],
etag: json["etag"] == null ? null : json["etag"],
nextPageToken: json["nextPageToken"] == null ? null : json["nextPageToken"],
regionCode: json["regionCode"] == null ? null : json["regionCode"],
pageInfo: json["pageInfo"] == null ? null : PageInfo.fromJson(json["pageInfo"]),
items: json["items"] == null ? null : new List<Item>.from(json["items"].map((x) => Item.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"kind": kind == null ? null : kind,
"etag": etag == null ? null : etag,
"nextPageToken": nextPageToken == null ? null : nextPageToken,
"regionCode": regionCode == null ? null : regionCode,
"pageInfo": pageInfo == null ? null : pageInfo.toJson(),
"items": items == null ? null : new List<dynamic>.from(items.map((x) => x.toJson())),
};
}
class Item {
String kind;
String etag;
Id id;
Snippet snippet;
Item({
this.kind,
this.etag,
this.id,
this.snippet,
});
factory Item.fromRawJson(String str) => Item.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Item.fromJson(Map<String, dynamic> json) => new Item(
kind: json["kind"] == null ? null : json["kind"],
etag: json["etag"] == null ? null : json["etag"],
id: json["id"] == null ? null : Id.fromJson(json["id"]),
snippet: json["snippet"] == null ? null : Snippet.fromJson(json["snippet"]),
);
Map<String, dynamic> toJson() => {
"kind": kind == null ? null : kind,
"etag": etag == null ? null : etag,
"id": id == null ? null : id.toJson(),
"snippet": snippet == null ? null : snippet.toJson(),
};
}
class Id {
String kind;
String videoId;
Id({
this.kind,
this.videoId,
});
factory Id.fromRawJson(String str) => Id.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Id.fromJson(Map<String, dynamic> json) => new Id(
kind: json["kind"] == null ? null : json["kind"],
videoId: json["videoId"] == null ? null : json["videoId"],
);
Map<String, dynamic> toJson() => {
"kind": kind == null ? null : kind,
"videoId": videoId == null ? null : videoId,
};
}
class Snippet {
DateTime publishedAt;
String channelId;
String title;
String description;
Thumbnails thumbnails;
Snippet({
this.publishedAt,
this.channelId,
this.title,
this.description,
this.thumbnails,
});
factory Snippet.fromRawJson(String str) => Snippet.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Snippet.fromJson(Map<String, dynamic> json) => new Snippet(
publishedAt: json["publishedAt"] == null ? null : DateTime.parse(json["publishedAt"]),
channelId: json["channelId"] == null ? null : json["channelId"],
title: json["title"] == null ? null : json["title"],
description: json["description"] == null ? null : json["description"],
thumbnails: json["thumbnails"] == null ? null : Thumbnails.fromJson(json["thumbnails"]),
);
Map<String, dynamic> toJson() => {
"publishedAt": publishedAt == null ? null : publishedAt.toIso8601String(),
"channelId": channelId == null ? null : channelId,
"title": title == null ? null : title,
"description": description == null ? null : description,
"thumbnails": thumbnails == null ? null : thumbnails.toJson(),
};
}
class Thumbnails {
Default thumbnailsDefault;
Default medium;
Thumbnails({
this.thumbnailsDefault,
this.medium,
});
factory Thumbnails.fromRawJson(String str) => Thumbnails.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Thumbnails.fromJson(Map<String, dynamic> json) => new Thumbnails(
thumbnailsDefault: json["default"] == null ? null : Default.fromJson(json["default"]),
medium: json["medium"] == null ? null : Default.fromJson(json["medium"]),
);
Map<String, dynamic> toJson() => {
"default": thumbnailsDefault == null ? null : thumbnailsDefault.toJson(),
"medium": medium == null ? null : medium.toJson(),
};
}
class Default {
String url;
int width;
int height;
Default({
this.url,
this.width,
this.height,
});
factory Default.fromRawJson(String str) => Default.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Default.fromJson(Map<String, dynamic> json) => new Default(
url: json["url"] == null ? null : json["url"],
width: json["width"] == null ? null : json["width"],
height: json["height"] == null ? null : json["height"],
);
Map<String, dynamic> toJson() => {
"url": url == null ? null : url,
"width": width == null ? null : width,
"height": height == null ? null : height,
};
}
class PageInfo {
int totalResults;
int resultsPerPage;
PageInfo({
this.totalResults,
this.resultsPerPage,
});
factory PageInfo.fromRawJson(String str) => PageInfo.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory PageInfo.fromJson(Map<String, dynamic> json) => new PageInfo(
totalResults: json["totalResults"] == null ? null : json["totalResults"],
resultsPerPage: json["resultsPerPage"] == null ? null : json["resultsPerPage"],
);
Map<String, dynamic> toJson() => {
"totalResults": totalResults == null ? null : totalResults,
"resultsPerPage": resultsPerPage == null ? null : resultsPerPage,
};
}
接下来做,
var data = await http.get("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={id}&maxResults=5&order=date&type=video&key={key}");
var jsonData = json.decode(data.body);
return Result.fromJson(jsonData);
此时它应该可以工作