我正在尝试用 flutter(使用 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) . . .
main.dart 中 tapScreen = ...
调用产生的错误:
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.
解决方法是向类构造函数添加大括号以明确指定命名参数,per this reference.
PageTapScreen({
this.key,
this.profileList,
this.tapScreenContext}) : super(key: key)
同样,我在这里发帖的唯一原因是强调对原始文章中指出的错误代码的潜在修复。根据错误消息,Thia 修复可能不直观。
关于dart - “需要 1 个必需参数,但找到 0 个”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51667637/