0

I am fairly new in Flutter. I have an issue where I am stock. I am trying to parse data from one widget to another StatefulWidget.

I have this widget where I try to parse data from

class MaltInput extends StatefulWidget {
  @override
  _MaltInputState createState() => _MaltInputState();
}

class _MaltInputState extends State<MaltInput> {

  List<String> malt = ['Malt 1', 'Malt 2', 'Malt 3'];
  String maltpick = "";


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Malt input'),

      ),
      body: ListView.builder(
        itemCount: malt.length,
        itemBuilder: (context, index){
          return Card(
            child: ListTile(
              onTap: (){
                Navigator.push(
                    context,
                    MaterialPageRoute(
                    builder: (context) => Test(malt[index]),
                ));
              },
              title: Text(malt[index]),
            ),
          );
        },
      ),
    );
  }
}

Parse to this widget

class Test extends StatefulWidget {

  String malt;
  Test({this.malt});

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


class _TestState extends State<Test> {
  String malt;
  _TestState({this.malt});

  List<String> items = [];

  final TextEditingController ectrl = TextEditingController();
  
  @override

  Widget build(BuildContext context) {

    String maltpick;
    maltpick = (widget.malt);

    //widget.malt = "";

    return Scaffold(
      appBar: AppBar(
        title: Text('Dynamic content'),
      ),
      body: Column(
        children: <Widget>[
//
        RaisedButton(
          child: Text('Add malt'),
          onPressed: (){
            Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => MaltInput()));
          }
        ),


          Text('Header.....'),
          Text(maltpick),

          Expanded(
            child: ListView.builder(
                itemCount: items.length,
                itemBuilder: (BuildContext ctxt, int Index){
                  return Text(items[Index]);
                }
            ),),
        ],
      ),
    );
  }
}

The error is in this line : builder: (context) => Test(malt[index]), Error code: Error: Too many positional arguments: 0 allowed, but 1 found. Try removing the extra positional arguments. builder: (context) => Test(malt[index]),

kyed
  • 61
  • 1
  • 7

2 Answers2

0

If you use named parameters - the ones in {} - in your constructor

Test({this.malt});

you need to invoke it like this

MaterialPageRoute(builder: (context) => Test(malt: malt[index]))

You can check the documentation on the different kinds of parameters here.

kazume
  • 1,333
  • 2
  • 16
  • 30
0

Replace the below code.

class Test extends StatefulWidget {

  String malt;
  Test({this.malt});// here I Changed

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

To

class Test extends StatefulWidget {

  String malt;
  Test(this.malt);// here it will be

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

And remove String malt; and _TestState({this.malt}); code from test class.

class _TestState extends State<Test> {
  String malt;// remove this line
  _TestState({this.malt});// remove this line too
Salim Murshed
  • 1,423
  • 1
  • 8
  • 19