dart - “需要 1 个必需参数,但找到 0 个”错误

标签 dart flutter flutter-layout

我正在尝试用 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/

相关文章:

android - flutter ios无法运行,获取widgets库异常并丢失设备连接

flutter - 为什么未定义“共享 flutter ”软件包?

dart - flutter 自定义画家

flutter - 如何在 showModalBottomSheet 的左侧、右侧和底部显示边距?

service - Flutter - Dart 中的后台服务(仅当应用程序可见时)

intellij-idea - 如何启用 Flutter/Dart 语言实验?

json - 如何在Flutter中读取本地JSON导入?

constructor - Dart Factory构造函数-与 “const”构造函数有何不同

flutter - CustomPainter - shouldRepaint 值得吗?

flutter - 修复底部空间的小部件