Yakoub Tarkaoui
Yakoub Tarkaoui

Reputation: 113

Error: Only static members can be accessed in initializers in flutter

I trying to resolve this problem but I can't I'm passing data to the constructor for initializing and put in a source can someone help me, please

plugin: 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();
  }

Upvotes: 0

Views: 126

Answers (1)

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27137

You can only access static members initialisation. To make variable static you use static keyword in-front of variable declaration.

static String url;

but if you. don't want to make it static then you can initialise that variable in initistate in following way.

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);
  }

Upvotes: 1

Related Questions