dart - “预期 1 个必需参数,但找到 0 个”错误
问题描述
我正在尝试用颤振构建一个应用程序(它使用 Dart 2)。我收到一个我无法弄清楚的错误:
错误在child: new Center(
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Begin with a search...'),
actions: <Widget> [
new FlatButton(
child: new Text('Logout', style: new TextStyle(fontSize: 17.0, color: Colors.white)),
onPressed: _signOut,
)
]
),
body: new Container(
margin: EdgeInsets.symmetric(horizontal: 20.0),
child: new Center(
child: new Row(
children: <Widget>[
new TextField(
controller: _controller,
autofocus: true,
textAlign: TextAlign.start,
decoration: new InputDecoration(
prefixIcon: new Padding(
padding: const EdgeInsetsDirectional.only(end: 10.0),
child: new Icon(Icons.search),
),
border: new OutlineInputBorder(borderRadius: new BorderRadius.all(Radius.circular(20.0))),
hintText: 'Search for a recipient',
),
onChanged: null,
),
new FlatButton(
onPressed: () {_controller.clear();},
child: new Icon(Icons.clear)
),
],
),
),
),
bottomNavigationBar: NavBar(),
);
}
解决方案
我在这个错误上度过了一段糟糕的时光(显然与通过上面的评论更正的根本原因不同。)。此处提供的信息以防其他人根据错误代码找到此帖子。
就我而言,我的构造函数格式不正确...
在 main.dart...
@override
void initState() {
tapScreen = PageTapScreen(
key : keyOne,
profileList : demoProfiles,
tapScreenContext : tapScreenContext,
); . . .
在我的代码库的其他地方:
class PageTapScreen extends StatefulWidget {
final Key key;
final List<Profile> profileList;
final BuildContext tapScreenContext;
PageTapScreen(
this.key,
this.profileList,
this.tapScreenContext) : super(key: key) . . .
tapScreen = ...
main.dart 中调用产生的错误:
3 required argument(s) expected, but 0 found.
The named parameter 'key' isn't defined.
The named parameter 'profileList' isn't defined.
The named parameter 'tapScreenContext' isn't defined.
修复方法是在 Class 构造函数中添加大括号,以根据此引用清楚地指定命名参数。
PageTapScreen({
this.key,
this.profileList,
this.tapScreenContext}) : super(key: key)
同样,我在这里发布的唯一原因是强调对原始文章中记录的错误代码的潜在修复。根据错误消息,Thia 修复可能不直观。
推荐阅读
- perl - “在数字 eq (==) 中使用未初始化的值”在 perl 脚本的特定行报告
- java - 如何访问在另一个类中定义的 java.util.prefs.Preferences 对象
- duplicates - 如何在此功率数据透视表中编写一个仅对唯一值旁边的值求和的度量?
- html - 我如何包装按钮?
- powershell - 如何在PowerShell中更新集群?
- windows - 从带有 PDB 的 DLL 在 Visual Studio 中显示枚举常量
- php - 在 PHP 中获取当前页面上的 h1 标签
- azure-data-factory - 获取管道名称及其活动
- object - 如何从 Kotlin 中的函数返回对象?
- python - 拥抱面部标记器无法正确加载文件