“预计需要1个自变量,但找到0个”错误

时间:2018-08-03 07:43:17

标签: 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(),
    );
  } 

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

这个错误让我度过了一个糟糕的时光(显然,根本原因与上面的注释所纠正的原因不同)。如果其他人根据错误代码找到了此帖子,请在此处提供信息。

在我的情况下,我的构造器格式不佳...

在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 :(得分:0)

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

 @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');
 }


}

答案 3 :(得分:0)

原因可能很少。我要添加我自己的愚蠢原因(。

错误构造函数

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});

谢谢!希望能回答您的问题!