Navigator.push 在 flutter 中有三个参数

Navigator.push with three parameters in flutter

我有一个带有三个下拉按钮的表单,这些按钮在 class、

的顶部声明
String _currentItemSelected1 = 'low';
String _currentItemSelected2 = 'low';
String _currentItemSelected3 = 'low';

然后我有 RisedButton 和 onPressed 属性 这个主体:

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

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

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

现在,问题是当我调用 SuggestionResult class 时它说:“太多的位置参数:预期 0,但找到 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);

试试这个:

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

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

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