【问题标题】:Navigator.push with three parameters in flutter颤振中带有三个参数的 Navigator.push
【发布时间】:2021-02-06 02:01:54
【问题描述】:

我有一个带有三个下拉按钮的表单,这些按钮是在类的顶部声明的,

String _currentItemSelected1 = 'low';
String _currentItemSelected2 = 'low';
String _currentItemSelected3 = 'low';

然后我有 RisedButton 和这个 body 的 onPressed 属性:

Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => SuggestionResult(
      _currentItemSelected1,
      _currentItemSelected2,
      _currentItemSelected3,
    ),
  ),
);

在 SuggestionResult 类中,我重写了构造函数,如下所示:

final String temp;
final String hum;
final String light;
SuggestionResult({this.temp, this.hum, this.light});

现在,问题是当我调用 SuggestionResult 类时,它说:“位置参数太多:预期为 0,但找到了 3。 尝试删除额外的位置参数,或指定命名参数的名称。”

【问题讨论】:

  • 你需要了解 dart 中位置参数和命名参数的区别。看看this question

标签: flutter dart parameters navigation


【解决方案1】:

当您调用 SuggestionResult 页面时,您应该指定参数的名称

...
builder: (context) => new SuggestionResult(
    temp: _currentItemSelected1,
    hum: _currentItemSelected2,
    light: _currentItemSelected3,
);
...

【讨论】:

    【解决方案2】:

    试试这个:

    new SuggestionResult(temp: _currentItemSelected1, hum: _currentItemSelected2, light: _currentItemSelected3);
    

    【讨论】:

      【解决方案3】:

      试试这个;

      onPressed: () {
                   Navigator.push(
                  context,
                 MaterialPageRoute(
                builder: (context) => new SuggestionResult(temp:_currentItemSelected1, hum: _currentItemSelected2, light: _currentItemSelected3  
               ),
             ),
           );
        },
      

      在 Dart 中,我们通过用花括号 ({}) 括起来来定义命名参数: 喜欢以下;

      SuggestionResult({this.temp, this.hum, this.light});
      

      这意味着我们可以像这样创建上面的小部件:

      new SuggestionResult(temp: _currentItemSelected1, hum: _currentItemSelected2, light: _currentItemSelected3);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-29
        • 1970-01-01
        • 2021-04-03
        • 2021-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多