错误:在flutter中只能在初始化程序中访问静态成员

时间:2020-03-11 02:14:06

标签: flutter dart

我试图解决此问题,但是我无法 我正在将数据传递给构造函数进行初始化并放入源中 有人可以帮我吗

插件:https://pub.dev/packages/flutter_ijkplayer

class FullScreen extends StatefulWidget {
  String url_ , userAgent;
  FullScreen(
    this.url_,
    this.userAgent,
  ) ;

  @override
  _FullScreenState createState() => _FullScreenState(this.url_, this.userAgent);
}

class _FullScreenState extends State<FullScreen> {
  var controller = IjkMediaController();
  String url , userAgent;


  _FullScreenState(this.url, this.userAgent);
  Orientation get orientation => MediaQuery.of(context).orientation;
  DataSource source = DataSource.network(
    widget.url_,------>ERROR : Error: Only static members can be accessed in initializers
    headers: {
      'User-Agent':widget.userAgent------>ERROR : Error: Only static members can be accessed in initializers
    }
  );


  @override
  initState() async {
    controller.setDataSource(source, autoPlay: true);
    super.initState();
  }

1 个答案:

答案 0 :(得分:1)

您只能访问静态成员初始化。要使变量成为静态变量,请在变量声明的前面使用static关键字。

静态字符串网址;

但是,如果你愿意。不想将其设为静态,则可以按以下方式在initistate中初始化该变量。

var controller;
  Orientation get orientation => MediaQuery.of(context).orientation;
  DataSource source;
  @override
  void initState() {
    super.initState();
    controller = IjkMediaController();
    source =
        DataSource.network(widget.url_, headers: {'User-Agent': widget.url_});
    controller.setDataSource(source, autoPlay: true);
  }