【问题标题】:why the variables become null?为什么变量变为空?
【发布时间】:2021-03-13 00:22:20
【问题描述】:

在颤振网络中,当我使用 WebVideoWidget 时,就像这样。但总是得到“视频在 toPlay 中为空”。

    Center(
      child: _video = WebVideoWidget(
                    //...
      ),
    )

    onTap: () {
      setState(() {
        _video.src = _urlClip;
      });
   }

WebVideoWidget 源文件

class WebVideoWidget extends StatefulWidget {
  final _WebVideoWidgetState _state = _WebVideoWidgetState();

  @override
  _WebVideoWidgetState createState() => _state;
    
      set src(String value) {
        //this.src = value;
        _state.toPlay(src);
      }
    
    }
    
    class _WebVideoWidgetState extends State<WebVideoWidget> {
      VideoElement _video;
      Widget _videoWidget;
      
      @override
      void initState() {
        super.initState();
        _video = VideoElement();
        ui.platformViewRegistry.registerViewFactory('webVideoElement', (int viewId) {
            //....
         return _video;
        }); 
        _videoWidget = HtmlElementView(key: UniqueKey(), viewType: 'webVideoElement');
      } 
      
      void toPlay(String url){
        if( _video==null )
          print('--------video is null in toPlay -------');
        else
          print('--------video is not null in toPlay-------');  
      }
      
      @override
      Widget build(BuildContext context) {
        return Stack(
          children: [
            SizedBox(
              width: widget.width.toDouble(),
              height: widget.height.toDouble(),
              child: _videoWidget,
            ),
          ],
        );
      }
    } 

  

为什么 toPlay 函数中的所有属性变量都为空?我实在想不通这个奇怪的问题。 html问题?

【问题讨论】:

  • 你能显示你的网址吗?
  • @SaifulIslam,我从后端获取 url。像这样:localhost:3000/video_clips/…
  • 也许我应该构造一个控制器并将其传递给子小部件来解决它?
  • 在您的问题中包含完整的错误日志,以帮助更多人理解。

标签: flutter flutter-web


【解决方案1】:

正确的方法是构造一个控制器。 代码示例在这里,希望能有所帮助。

    class WebVideoController {
      _WebVideoWidgetState _videoState;
    
      void setParent(_WebVideoWidgetState videoState) {
        this._videoState = videoState;
      }
    
      void play(url){
        this._videoState.toPlay(url);
      }
    }

  @override
  _WebVideoWidgetState createState() => _WebVideoWidgetState(controller);

  _WebVideoWidgetState(this.controller){
    this.controller.setParent(this);
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 2015-09-05
    • 2018-01-16
    • 2015-12-02
    • 1970-01-01
    相关资源
    最近更新 更多