我正在使用chewie包来显示和管理演示视频。我需要初始化VideoPlayerController在事件来到应用程序时,视频演示已经开始(它返回给我的视频演示的URL,我可以将其传递到VideoPlayerController.network(URL))。但它抛出了一个错误,在开始之前,变量没有初始化开始演示(这是显而易见的)。有没有办法把它初始化为一个空对象,然后在以后重写它或者类似的方法?late变量是需要的,因为它在app中很少使用,比如initState和build方法。
部分代码有助于理解我的意思:
late VideoPlayerController videoPlayerController;
@override
void initState() {
_onVideoPresentationChangeSubscription = instance
.videoPresentation
.onVideoPresentationChange()
.listen((event) {
if (event.type == VideoPresentationEventNames.videoPresentationStarted) {
videoPlayerController = VideoPlayerController.network(url)..initialize();
}
});
}
@override
Widget build(BuildContext context) {
final chewieController = ChewieController(
videoPlayerController: videoPlayerController,
autoPlay: true,
looping: true,
showControls: false,
);
final playerWidget = Chewie(
controller: chewieController,
);
return SizedBox(
width: MediaQuery.of(context).size.width * 0.9,
height: MediaQuery.of(context).size.height * 0.3,
child: AspectRatio(
aspectRatio: videoPlayerController.value.aspectRatio,
child: playerWidget,
),
);
}
1条答案
按热度按时间pcww981p1#
你应该在initState重写中初始化你的控制器。然后在build方法中,你应该只有当控制器存在并且videoPlayerController值初始化时才有条件地呈现Chewie播放器。在videoPlayerController没有准备好的时候,只显示一个正在加载的小部件。