'My youtube Video Player is not working with listview.builder
when i tried youtube credential for youtube video player with list view it is not working.
showing The following error:
NoSuchMethodError was thrown building HomeScreen(dirty, state: _HomeScreenState#44f2e): The getter 'video' was called on null. Receiver: null
i want to play my youtube video with in a page with my channel info and video list of the channel.
My code is as below-
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:HolyTune/service/videolist.dart';
import '../models/chanelinfo.dart';
import '../service/videolist.dart';
import '../service/videoplayer.dart';
import '../widgets/service.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
class HomeScreen extends StatefulWidget {
HomeScreen({this.videoItem});
final VideoItem videoItem;
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
//
ChannelInfo _channelInfo;
VideosList _videosList;
Item _item;
bool _loading;
String _playListId;
String _nextPageToken;
ScrollController _scrollController;
YoutubePlayerController _controller;
bool _isPlayerReady;
// final VideoItem videoItem;
@override
void initState() {
super.initState();
_loading = true;
_nextPageToken = '';
_scrollController = ScrollController();
_videosList = VideosList();
_videosList.videos = List();
_getChannelInfo();
_isPlayerReady = false;
_controller = YoutubePlayerController(
initialVideoId: widget.videoItem.video.resourceId.videoId,
flags: YoutubePlayerFlags(
mute: false,
autoPlay: true,
),
)..addListener(_listener);
}
void _listener() {
if (_isPlayerReady && mounted && !_controller.value.isFullScreen) {
//
}
}
// @override
// void deactivate() {
// _controller.pause();
// super.deactivate();
// }
// @override
// void dispose() {
// _controller.dispose();
// super.dispose();
// }
// @override
// void initState() {
// super.initState();
// _loading = true;
// _nextPageToken = '';
// _scrollController = ScrollController();
// _videosList = VideosList();
// _videosList.videos = List();
// _getChannelInfo();
// }
_getChannelInfo() async {
_channelInfo = await Services.getChannelInfo();
_item = _channelInfo.items[0];
_playListId = _item.contentDetails.relatedPlaylists.uploads;
print('_playListId $_playListId');
await _loadVideos();
setState(() {
_loading = false;
});
}
_loadVideos() async {
VideosList tempVideosList = await Services.getVideosList(
playListId: _playListId,
pageToken: _nextPageToken,
);
_nextPageToken = tempVideosList.nextPageToken;
_videosList.videos.addAll(tempVideosList.videos);
print('videos: ${_videosList.videos.length}');
print('_nextPageToken $_nextPageToken');
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.videoItem.video.title),
),
body: Column(
children: [
_buildInfoView(),
// Container(
// child:
// ),
Expanded(
child: NotificationListener<ScrollEndNotification>(
onNotification: (ScrollNotification notification) {
if (_videosList.videos.length >=
int.parse(_item.statistics.videoCount)) {
return true;
}
if (notification.metrics.pixels ==
notification.metrics.maxScrollExtent) {
_loadVideos();
}
return true;
},
child: ListView.builder(
// shrinkWrap: true,
controller: _scrollController,
itemCount: _videosList.videos.length,
itemBuilder: (context, index) {
VideoItem videoItem = _videosList.videos[index];
return InkWell(
onTap: () async {
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return YoutubePlayer(
//videoItem: videoItem,
);
}));
},
child: Container(
padding: EdgeInsets.all(20.0),
child: Row(
children: [
CachedNetworkImage(
imageUrl: videoItem
.video.thumbnails.thumbnailsDefault.url,
),
SizedBox(width: 20),
Flexible(child: Text(videoItem.video.title)),
],
),
),
);
},
),
),
),
],
),
);
}
_buildInfoView() {
return YoutubePlayer(
controller: _controller,
showVideoProgressIndicator: true,
onReady: () {
print('Player is ready.');
_isPlayerReady = true;
},
);
}
}
Videolist.dart file
// To parse this JSON data, do
//
// final videosList = videosListFromJson(jsonString);
import 'dart:convert';
VideosList videosListFromJson(String str) =>
VideosList.fromJson(json.decode(str));
String videosListToJson(VideosList data) => json.encode(data.toJson());
class VideosList {
VideosList({
this.kind,
this.etag,
this.nextPageToken,
this.videos,
this.pageInfo,
});
String kind;
String etag;
String nextPageToken;
List<VideoItem> videos;
PageInfo pageInfo;
factory VideosList.fromJson(Map<String, dynamic> json) => VideosList(
kind: json["kind"],
etag: json["etag"],
nextPageToken: json["nextPageToken"],
videos: List<VideoItem>.from(
json["items"].map((x) => VideoItem.fromJson(x))),
pageInfo: PageInfo.fromJson(json["pageInfo"]),
);
Map<String, dynamic> toJson() => {
"kind": kind,
"etag": etag,
"nextPageToken": nextPageToken,
"items": List<dynamic>.from(videos.map((x) => x.toJson())),
"pageInfo": pageInfo.toJson(),
};
}
class VideoItem {
VideoItem({
this.kind,
this.etag,
this.id,
this.video,
});
String kind;
String etag;
String id;
Video video;
factory VideoItem.fromJson(Map<String, dynamic> json) => VideoItem(
kind: json["kind"],
etag: json["etag"],
id: json["id"],
video: Video.fromJson(json["snippet"]),
);
Map<String, dynamic> toJson() => {
"kind": kind,
"etag": etag,
"id": id,
"snippet": video.toJson(),
};
}
class Video {
Video({
this.publishedAt,
this.channelId,
this.title,
this.description,
this.thumbnails,
this.channelTitle,
this.playlistId,
this.position,
this.resourceId,
});
DateTime publishedAt;
String channelId;
String title;
String description;
Thumbnails thumbnails;
String channelTitle;
String playlistId;
int position;
ResourceId resourceId;
factory Video.fromJson(Map<String, dynamic> json) => Video(
publishedAt: DateTime.parse(json["publishedAt"]),
channelId: json["channelId"],
title: json["title"],
description: json["description"],
thumbnails: Thumbnails.fromJson(json["thumbnails"]),
channelTitle: json["channelTitle"],
playlistId: json["playlistId"],
position: json["position"],
resourceId: ResourceId.fromJson(json["resourceId"]),
);
Map<String, dynamic> toJson() => {
"publishedAt": publishedAt.toIso8601String(),
"channelId": channelId,
"title": title,
"description": description,
"thumbnails": thumbnails.toJson(),
"channelTitle": channelTitle,
"playlistId": playlistId,
"position": position,
"resourceId": resourceId.toJson(),
};
}
class ResourceId {
ResourceId({
this.kind,
this.videoId,
});
String kind;
String videoId;
factory ResourceId.fromJson(Map<String, dynamic> json) => ResourceId(
kind: json["kind"],
videoId: json["videoId"],
);
Map<String, dynamic> toJson() => {
"kind": kind,
"videoId": videoId,
};
}
class Thumbnails {
Thumbnails({
this.thumbnailsDefault,
this.medium,
this.high,
this.standard,
this.maxres,
});
Default thumbnailsDefault;
Default medium;
Default high;
Default standard;
Default maxres;
factory Thumbnails.fromJson(Map<String, dynamic> json) => Thumbnails(
thumbnailsDefault: Default.fromJson(json["default"]),
medium: Default.fromJson(json["medium"]),
high: Default.fromJson(json["high"]),
standard: null == json["standard"]
? null
: Default.fromJson(json["standard"]),
maxres:
null == json["maxres"] ? null : Default.fromJson(json["maxres"]),
);
Map<String, dynamic> toJson() => {
"default": thumbnailsDefault.toJson(),
"medium": medium.toJson(),
"high": high.toJson(),
"standard": standard.toJson(),
"maxres": maxres.toJson(),
};
}
class Default {
Default({
this.url,
this.width,
this.height,
});
String url;
int width;
int height;
factory Default.fromJson(Map<String, dynamic> json) => Default(
url: json["url"],
width: json["width"],
height: json["height"],
);
Map<String, dynamic> toJson() => {
"url": url,
"width": width,
"height": height,
};
}
class PageInfo {
PageInfo({
this.totalResults,
this.resultsPerPage,
});
int totalResults;
int resultsPerPage;
factory PageInfo.fromJson(Map<String, dynamic> json) => PageInfo(
totalResults: json["totalResults"],
resultsPerPage: json["resultsPerPage"],
);
Map<String, dynamic> toJson() => {
"totalResults": totalResults,
"resultsPerPage": resultsPerPage,
};
}
Channelinfo.dart file
// To parse this JSON data, do
//
// final channelInfo = channelInfoFromJson(jsonString);
import 'dart:convert';
ChannelInfo channelInfoFromJson(String str) =>
ChannelInfo.fromJson(json.decode(str));
String channelInfoToJson(ChannelInfo data) => json.encode(data.toJson());
class ChannelInfo {
ChannelInfo({
this.kind,
this.etag,
this.pageInfo,
this.items,
});
String kind;
String etag;
PageInfo pageInfo;
List<Item> items;
factory ChannelInfo.fromJson(Map<String, dynamic> json) => ChannelInfo(
kind: json["kind"],
etag: json["etag"],
pageInfo: PageInfo.fromJson(json["pageInfo"]),
items: List<Item>.from(json["items"].map((x) => Item.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"kind": kind,
"etag": etag,
"pageInfo": pageInfo.toJson(),
"items": List<dynamic>.from(items.map((x) => x.toJson())),
};
}
class Item {
Item({
this.kind,
this.etag,
this.id,
this.snippet,
this.contentDetails,
this.statistics,
});
String kind;
String etag;
String id;
Snippet snippet;
ContentDetails contentDetails;
Statistics statistics;
factory Item.fromJson(Map<String, dynamic> json) => Item(
kind: json["kind"],
etag: json["etag"],
id: json["id"],
snippet: Snippet.fromJson(json["snippet"]),
contentDetails: ContentDetails.fromJson(json["contentDetails"]),
statistics: Statistics.fromJson(json["statistics"]),
);
Map<String, dynamic> toJson() => {
"kind": kind,
"etag": etag,
"id": id,
"snippet": snippet.toJson(),
"contentDetails": contentDetails.toJson(),
"statistics": statistics.toJson(),
};
}
class ContentDetails {
ContentDetails({
this.relatedPlaylists,
});
RelatedPlaylists relatedPlaylists;
factory ContentDetails.fromJson(Map<String, dynamic> json) => ContentDetails(
relatedPlaylists: RelatedPlaylists.fromJson(json["relatedPlaylists"]),
);
Map<String, dynamic> toJson() => {
"relatedPlaylists": relatedPlaylists.toJson(),
};
}
class RelatedPlaylists {
RelatedPlaylists({
this.likes,
this.favorites,
this.uploads,
this.watchHistory,
this.watchLater,
});
String likes;
String favorites;
String uploads;
String watchHistory;
String watchLater;
factory RelatedPlaylists.fromJson(Map<String, dynamic> json) =>
RelatedPlaylists(
likes: json["likes"],
favorites: json["favorites"],
uploads: json["uploads"],
watchHistory: json["watchHistory"],
watchLater: json["watchLater"],
);
Map<String, dynamic> toJson() => {
"likes": likes,
"favorites": favorites,
"uploads": uploads,
"watchHistory": watchHistory,
"watchLater": watchLater,
};
}
class Snippet {
Snippet({
this.title,
this.description,
this.publishedAt,
this.thumbnails,
this.localized,
this.country,
});
String title;
String description;
DateTime publishedAt;
Thumbnails thumbnails;
Localized localized;
String country;
factory Snippet.fromJson(Map<String, dynamic> json) => Snippet(
title: json["title"],
description: json["description"],
publishedAt: DateTime.parse(json["publishedAt"]),
thumbnails: Thumbnails.fromJson(json["thumbnails"]),
localized: Localized.fromJson(json["localized"]),
country: json["country"],
);
Map<String, dynamic> toJson() => {
"title": title,
"description": description,
"publishedAt": publishedAt.toIso8601String(),
"thumbnails": thumbnails.toJson(),
"localized": localized.toJson(),
"country": country,
};
}
class Localized {
Localized({
this.title,
this.description,
});
String title;
String description;
factory Localized.fromJson(Map<String, dynamic> json) => Localized(
title: json["title"],
description: json["description"],
);
Map<String, dynamic> toJson() => {
"title": title,
"description": description,
};
}
class Thumbnails {
Thumbnails({
this.thumbnailsDefault,
this.medium,
this.high,
});
Default thumbnailsDefault;
Default medium;
Default high;
factory Thumbnails.fromJson(Map<String, dynamic> json) => Thumbnails(
thumbnailsDefault: Default.fromJson(json["default"]),
medium: Default.fromJson(json["medium"]),
high: Default.fromJson(json["high"]),
);
Map<String, dynamic> toJson() => {
"default": thumbnailsDefault.toJson(),
"medium": medium.toJson(),
"high": high.toJson(),
};
}
class Default {
Default({
this.url,
this.width,
this.height,
});
String url;
int width;
int height;
factory Default.fromJson(Map<String, dynamic> json) => Default(
url: json["url"],
width: json["width"],
height: json["height"],
);
Map<String, dynamic> toJson() => {
"url": url,
"width": width,
"height": height,
};
}
class Statistics {
Statistics({
this.viewCount,
this.commentCount,
this.subscriberCount,
this.hiddenSubscriberCount,
this.videoCount,
});
String viewCount;
String commentCount;
String subscriberCount;
bool hiddenSubscriberCount;
String videoCount;
factory Statistics.fromJson(Map<String, dynamic> json) => Statistics(
viewCount: json["viewCount"],
commentCount: json["commentCount"],
subscriberCount: json["subscriberCount"],
hiddenSubscriberCount: json["hiddenSubscriberCount"],
videoCount: json["videoCount"],
);
Map<String, dynamic> toJson() => {
"viewCount": viewCount,
"commentCount": commentCount,
"subscriberCount": subscriberCount,
"hiddenSubscriberCount": hiddenSubscriberCount,
"videoCount": videoCount,
};
}
class PageInfo {
PageInfo({
this.resultsPerPage,
});
int resultsPerPage;
factory PageInfo.fromJson(Map<String, dynamic> json) => PageInfo(
resultsPerPage: json["resultsPerPage"],
);
Map<String, dynamic> toJson() => {
"resultsPerPage": resultsPerPage,
};
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|