我想从这个函数返回一个 videoController 的值,并将它作为参数传递给另一个。当我在这个函数中打印值时,我得到了我期望的值,但是当将它传递给另一个并打印它时,我得到了空值。
Future<VideoPlayerController> _startVideoPlayer() async {
VideoPlayerController vController =
VideoPlayerController.file(File(videoFile.path));
videoPlayerListener = () {
if (videoController != null && videoController.value.size != null) {
// Refreshing the state to update video player with the correct ratio.
if (mounted) setState(() {});
videoController.removeListener(videoPlayerListener);
}
};
vController.addListener(videoPlayerListener);
await vController.setLooping(true);
await vController.initialize();
await videoController?.dispose();
if (mounted) {
setState(() {
imageFile = null;
videoController = vController;
});
}
await vController.play();
return videoController;
}
答案 0 :(得分:0)
试试
Future<VideoPlayerController> _startVideoPlayer() async {
VideoPlayerController vController =
VideoPlayerController.file(File(videoFile.path));
videoPlayerListener = () {
if (videoController != null && videoController.value.size != null) {
// Refreshing the state to update video player with the correct ratio.
if (mounted) setState(() {});
videoController.removeListener(videoPlayerListener);
}
return videoPlayerListener;
};
vController.addListener( await _startVideoPlayer());
await vController.setLooping(true);
await vController.initialize();
await videoController?.dispose();
if (mounted) {
setState(() {
imageFile = null;
videoController = vController;
});
}
await vController.play();
return videoController;