flutter - 颤振中带有三个参数的 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 个。尝试删除额外的位置参数,或指定命名参数的名称。”
解决方案
试试这个;
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);
推荐阅读
- sql - 如何在更新查询中使用 SUBSTR
- android - 底部应用栏导航图标不是垂直居中
- python - 使用 python + sqlanydb 在 SAP IQ 上加载数据。'由于文件权限,文件操作失败
- apache - 为 Magento 在 Plesk(apache2、nginx 和 FPM)上设置 Varnish
- django - 在 home.html 中链接静态文件
- apache-spark - SecurityManager:禁用身份验证;ui acls 禁用;具有查看权限的用户:
- asp.net - Object Required 424 错误只发生在 IE11
- c# - 按名称动态获取常量的嵌套值
- python - Ubuntu16.04 python3.6.5无法安装tensorflow
- javascript - 如何从存在多个局部视图且每个局部视图没有提交按钮的视图中将值设置为 db?