Video.fromMap constructor
Implementation
factory Video.fromMap(Map<String, dynamic>? map) {
List<Thumbnail>? thumbnails;
if(map?.containsKey("videoRenderer") ?? false){
//Trending and search videos
var lengthText = map?['videoRenderer']?['lengthText'];
var simpleText =
map?['videoRenderer']?['shortViewCountText']?['simpleText'];
thumbnails = [];
map?['videoRenderer']
['thumbnail']['thumbnails']
.forEach((thumbnail) {
thumbnails!.add(Thumbnail(url: thumbnail['url'], width: thumbnail['width'], height: thumbnail['height']));
});
return Video(
videoId: map?['videoRenderer']?['videoId'],
duration: (lengthText == null) ? "Live" : lengthText?['simpleText'],
title: map?['videoRenderer']?['title']?['runs']?[0]?['text'],
channelName: map?['videoRenderer']['longBylineText']['runs'][0]['text'],
thumbnails: thumbnails,
views: (lengthText == null)
? "Views " +
map!['videoRenderer']['viewCountText']['runs'][0]['text']
: simpleText);
} else if (map?.containsKey("compactVideoRenderer")?? false){
//Related videos
thumbnails = [];
map?['compactVideoRenderer']
['thumbnail']['thumbnails']
.forEach((thumbnail) {
thumbnails!.add(Thumbnail(url: thumbnail['url'], width: thumbnail['width'], height: thumbnail['height']));
});
return Video(
videoId: map?['compactVideoRenderer']['videoId'],
title: map?['compactVideoRenderer']
?['title']?['simpleText'],
duration: map?['compactVideoRenderer']
?['lengthText']?['simpleText'],
thumbnails: thumbnails,
channelName: map?['compactVideoRenderer']
?['shortBylineText']?['runs']
?[0]?['text'],
views: map?['compactVideoRenderer']
?['viewCountText']?['simpleText']
);
} else if(map?.containsKey("gridVideoRenderer")?? false) {
String? simpleText = map?['gridVideoRenderer']
['shortViewCountText']?['simpleText'];
thumbnails = [];
map?['gridVideoRenderer']
['thumbnail']['thumbnails']
.forEach((thumbnail) {
thumbnails!.add(Thumbnail(url: thumbnail['url'], width: thumbnail['width'], height: thumbnail['height']));
});
return Video(
videoId: map?['gridVideoRenderer']['videoId'],
title: map?['gridVideoRenderer']['title']['runs'][0]
['text'],
duration: map?['gridVideoRenderer']['thumbnailOverlays'][0]
['thumbnailOverlayTimeStatusRenderer']['text']['simpleText'],
thumbnails: thumbnails,
views: (simpleText != null) ? simpleText : "???"
);
}
return Video();
}