首页 > 解决方案 > “预期 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(),
    );
  } 

标签: dartflutterflutter-layout

解决方案


我在这个错误上度过了一段糟糕的时光(显然与通过上面的评论更正的根本原因不同。)。此处提供的信息以防其他人根据错误代码找到此帖子。

就我而言,我的构造函数格式不正确...

在 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 修复可能不直观。


推荐阅读