【问题标题】:'1 required argument(s) expected but 0 found' error“预期 1 个必需参数,但找到 0 个”错误
【发布时间】:2019-01-11 01:15:57
【问题描述】:

我正在尝试使用 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(),
    );
  } 

【问题讨论】:

  • 你用的是什么 Flutter 版本?在项目目录中运行flutter analyze 时是否遇到同样的错误?能否请您添加完整的错误输出?
  • Flutter 版本 v0.5.1(测试版)。运行 flutter analyze 不会返回相同的错误。唯一的错误是{ "resource": "/Users/Samuel/Documents/GitHub/MyApp/lib/root_page.dart", "owner": "dart", "code": "missing_return", "severity": 4, "message": "This function declares a return type of 'Widget', but doesn't end with a return statement.", "source": "dart", "startLineNumber": 47, "startColumn": 5, "endLineNumber": 47, "endColumn": 11, "tags": [] }
  • 那么重启 IDE 应该已经解决了。您使用的是什么 IDE?
  • 使用 VSCode。我刚刚重新启动,它工作!谢谢...这么简单的解决方案哈哈
  • 很高兴听到:)

标签: dart flutter flutter-layout


【解决方案1】:

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

在我的例子中,我的构造函数格式不正确......

在 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.

解决方法是在 Class 构造函数中添加大括号以明确指定命名参数per this reference.

PageTapScreen({
    this.key,
    this.profileList,
    this.tapScreenContext}) : super(key: key)  

再次重申,我在这里发帖的唯一原因是强调对原始文章中指出的错误代码的潜在修复。根据错误消息,Thia 修复可能不直观。

【讨论】:

    【解决方案2】:

    可能有几个原因。我正在添加我自己的愚蠢理由(。

    错误构造函数

    const SingleShiftModel(
          this.parentId,
          {this.id,
          this.siteName,
          this.startTime,
          this.endTime,
          this.isDay,
          this.breakTimeInMins,
          this.isSigned,
          this.hourlyRate,
          this.date,
          this.comment,
          this.pngSignatureBytes});
    

    我改变了什么并且它起作用了? ->

    const SingleShiftModel(
          {this.parentId,
          this.id,
          this.siteName,
          this.startTime,
          this.endTime,
          this.isDay,
          this.breakTimeInMins,
          this.isSigned,
          this.hourlyRate,
          this.date,
          this.comment,
          this.pngSignatureBytes});
    

    谢谢!希望回答了你的问题!

    【讨论】:

      【解决方案3】:

      我认为您无意中更改了 Basic.dart 小部件。在(your_flutter_sdk/flutter/packages/flutter/lib/src/widgets/basic.dart)。 你需要一个新的。您需要重新安装 Flutter 包。

      【讨论】:

        【解决方案4】:

        所以问题出在响应上。所以我就这样解决了

         @override
         Future<AuthUser> doLogin(data) async {
         final response = await http.post('$baseUrl/user/login', body: data);
        
         if (response.statusCode == 200) {
           var data = json.decode(response.body); 
            print(data['success']['user']);
            return AuthUser.fromJson(data['success']['user']); 
         }else {
           throw new Exception('Failed To Login User');
         }
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 2019-03-18
          • 1970-01-01
          • 2021-07-29
          • 2021-08-22
          • 2019-08-28
          • 1970-01-01
          • 2021-06-04
          • 2021-12-18
          • 2021-07-27
          相关资源
          最近更新 更多