dart 错误:状态错误:调用close后无法添加新事件

yhuiod9q  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(191)

我正在使用“”youtube_player_iframe:^2.1.0'''用于在Flutter Web应用程序中显示YouTube视频的软件包。视频播放完全正常,但每当我点击全屏以全屏播放视频时,它都会出现此错误,而且它也不会使视频全屏播放并返回到以前的大小
错误

Error: Bad state: Cannot add new events after calling close
    at Object.throw_ [as throw] (http://localhost:42339/dart_sdk.js:5041:11)
    at _AsyncBroadcastStreamController.new.add (http://localhost:42339/dart_sdk.js:31586:44)
    at controller.YoutubePlayerController.new.add (http://localhost:42339/packages/youtube_player_iframe/src/helpers/youtube_value_builder.dart.lib.js:894:32)
    at http://localhost:42339/packages/youtube_player_iframe/src/helpers/youtube_value_builder.dart.lib.js:469:29
    at Object._checkAndCall (http://localhost:42339/dart_sdk.js:5246:16)
    at Object.dcall (http://localhost:42339/dart_sdk.js:5251:17)
    at http://localhost:42339/dart_sdk.js:100646:100
Error: Bad state: Cannot add new events after calling close
    at Object.throw_ [as throw] (http://localhost:42339/dart_sdk.js:5041:11)
    at _AsyncBroadcastStreamController.new.add (http://localhost:42339/dart_sdk.js:31586:44)
    at controller.YoutubePlayerController.new.add (http://localhost:42339/packages/youtube_player_iframe/src/helpers/youtube_value_builder.dart.lib.js:894:32)
    at http://localhost:42339/packages/youtube_player_iframe/src/helpers/youtube_value_builder.dart.lib.js:469:29
    at Object._checkAndCall (http://localhost:42339/dart_sdk.js:5246:16)
    at Object.dcall (http://localhost:42339/dart_sdk.js:5251:17)
    at http://localhost:42339/dart_sdk.js:100646:100

代码:

import 'package:flutter/foundation.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'package:youtube_player_iframe/youtube_player_iframe.dart';
    
    class YoutubePlayerWeb extends StatefulWidget {
      final String url;
    
      const YoutubePlayerWeb({Key? key, required this.url}) : super(key: key);
      
    
      @override
      _YoutubePlayerState createState() => _YoutubePlayerState();
    }
    
    class _YoutubePlayerState extends State<YoutubePlayerWeb> {
      late YoutubePlayerController _controller;
    
      void runYoutubePlay()
      {
        _controller = YoutubePlayerController(
          initialVideoId: YoutubePlayerController.convertUrlToId(widget.url).toString(),
          params: const YoutubePlayerParams(
            showControls: true,
            desktopMode: true,
            showFullscreenButton: true,
            privacyEnhanced: true,
            showVideoAnnotations: true ,
            autoPlay: false,
            enableCaption: true,
            color: 'red',
          )
        );
      }
    
      void youtubePlayerFullScreen()
      {
        _controller.onEnterFullscreen = ()
        {
          SystemChrome.setPreferredOrientations([
            DeviceOrientation.landscapeLeft,
            DeviceOrientation.landscapeRight,
          ]);
          print("ENTERED FULLSCREEN");
        };
        _controller.onExitFullscreen = ()
        {
          print("EXITED FULLSCREEN");
        };
      }
      
      @override
      void initState() {
          runYoutubePlay();
          youtubePlayerFullScreen();
        super.initState();
      }
    
      @override
      void dispose() {
        _controller.close();
        super.dispose();
      }
    
    
      @override
      Widget build(BuildContext context) {
        const player = YoutubePlayerIFrame();
        return YoutubePlayerControllerProvider(controller: _controller, child: player);
      }
    
    }

请帮帮我,请告诉我哪里错了?

tjrkku2a

tjrkku2a1#

此问题的原因是在dispose()上关闭事件后,尝试在YoutubePlayerController上使用该事件。可以使用检查器查看_controller是否仍处于打开状态。

if (!_controller.isClosed){
  // Add events
}

相关问题