如何在颤振中将值从一个函数传递到另一个函数?

时间:2021-03-09 23:03:11

标签: flutter dart

我想从这个函数返回一个 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;

}

1 个答案:

答案 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;