0

I want to pass two String variables to another class in flutter and used it. I tried but failed.

I tried this way but it said; Instance member can't be accessed using static. I want to know the right way to do it.

In MyHome class; I passed litemspage0[index] and litemsname0[index] to VideoPlayer0 class

    _getPage(int page) {
switch (page) {
  case 0:
    return ListView.builder(
      itemBuilder: (context, index) {
        return new Padding(
          padding:
              new EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
          child: new Card(
            elevation: 2.0,
            shape: RoundedRectangleBorder(
                borderRadius: new BorderRadius.circular(16.0)),
            child: InkWell(
              onTap: () {
                Navigator.of(context).push(
          new MaterialPageRoute(
            builder: (context) {
              return new VideoPlayer0(litemspage0[index],litemsname0[index]);
            },
          ),
        );

I want to use litemspage0[index],litemsname0[index] in VideoPlayer0 class. Here is the code I tried,

        class VideoPlayer0 extends StatefulWidget {

  final String litemspage0;
  final String litemsname0;

  VideoPlayer0(this.litemspage0, this.litemsname0);

  @override
  _MyHomePageState0 createState() => _MyHomePageState0();
}

class _MyHomePageState0 extends State<VideoPlayer0> {

  static const platform = const MethodChannel("www.youtube.com");
  TextEditingController _idController = TextEditingController();
  TextEditingController _seekToController = TextEditingController();
  double _volume = 1.0;
  VideoPlayerController _videoController;
  String position = "Get Current Position";
  String status = "Get Player Status";
  String videoDuration = "Get Video Duration";
  String _source = VideoPlayer0.litemspage0;
  bool isMute = false;

But I'm getting this error.

String _source = VideoPlayer0.litemspage0;

Instance member 'litemspage0' can't be accessed using static access.

I want to know the proper way to pass variables in one class to another in flutter.

3
  • 1
    To access state in stateful widget, you need to access by calling widget like widget.litemsname0 Commented Sep 16, 2019 at 12:06
  • I tried it. But getting an error. Only static members can be accessed in initializers. Commented Sep 16, 2019 at 12:25
  • Error in String _source = widget.litemspage0; widget Commented Sep 16, 2019 at 12:28

2 Answers 2

1

That's not the right way to pass variables to another class . the right way goes like this :

VideoPlayer0(litemspage0: yourPageValue  ,litemsname0:yourNameValue);

and you can access them in the state class like that :

String _source = widget.litemspage0;
Sign up to request clarification or add additional context in comments.

2 Comments

The constructor doesn't contains optional variables. In that case, named parameters in function calls are invalid!.
actually the constructor contain optional variables.so if you want it to be not optional you should add @required before this.variable
0

In your state class, in order to access the variables of the widget class, you’ll have to use widget. So in your case, use widget.litemspage0.

In addition, you’ll have to assign _source in initState() like so:

String _source;

@override
initState() {
  super.initState();
  _source = widget.litemspage0;
}

2 Comments

I tried it. But getting an error. Only static members can be accessed in initializers.
Error in String _source = widget.litemspage0; widget

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.