【问题标题】:Flutter - Parse data to statefulWidget [duplicate]Flutter - 将数据解析为 statefulWidget [重复]
【发布时间】:2021-01-13 12:05:09
【问题描述】:

我是 Flutter 的新手。我有一个库存问题。我正在尝试将数据从一个小部件解析到另一个 StatefulWidget。

我有这个小部件,我尝试从中解析数据

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

解析到这个小部件

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

错误在这一行:builder: (context) => Test(malt[index]), 错误代码:错误:位置参数太多:允许 0,但找到 1。 尝试删除额外的位置参数。 builder: (context) => Test(malt[index]),

【问题讨论】:

    标签: flutter


    【解决方案1】:

    如果您在构造函数中使用命名参数 - {} 中的参数

    Test({this.malt});
    

    你需要像这样调用它

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

    您可以查看有关不同类型参数here的文档。

    【讨论】:

      【解决方案2】:

      替换下面的代码。

      class Test extends StatefulWidget {
      
        String malt;
        Test({this.malt});// here I Changed
      
        @override
        _TestState createState() => _TestState();
      }
      

      class Test extends StatefulWidget {
      
        String malt;
        Test(this.malt);// here it will be
      
        @override
        _TestState createState() => _TestState();
      }
      

      并从测试类中删除 String malt;_TestState({this.malt}); 代码。

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

      【讨论】:

        猜你喜欢
        • 2020-02-25
        • 2018-10-19
        • 2019-10-08
        • 2020-04-11
        • 2021-12-09
        • 1970-01-01
        • 1970-01-01
        • 2020-05-09
        • 2021-08-18
        相关资源
        最近更新 更多