首页 > 解决方案 > 颤振中带有三个参数的 Navigator.push

问题描述

我有三个下拉按钮的表格,这些按钮是在班级顶部声明的,

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

然后我在这个主体上有带有 onPressed 属性的 RisedButton:

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 个。尝试删除额外的位置参数,或指定命名参数的名称。”

标签: flutterdartparametersnavigation

解决方案


试试这个;

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

推荐阅读