Dart:错误:未找到Setter(在定义类的构造函数时)

时间:2019-07-11 00:39:55

标签: flutter dart constructor this super

使用这个非常简单的构造函数会出现以下错误:

class MyHomePage extends StatefulWidget {
  MyHomePage(String title, String lang, {Key key}) {
    this.title = title;
    this.lang = lang.substring(0,2).toLowerCase();
    super(key: key);
  }


  final String title;
  final String lang;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

错误消息:

Compiler message:                                                       
lib/main.dart:34:10: Error: Setter not found: 'title'.                  
    this.title = title;                                                 
         ^^^^^                                                          
lib/main.dart:35:10: Error: Setter not found: 'lang'.                   
    this.lang = lang.substring(0,2).toLowerCase();                      
         ^^^^                                                           
lib/main.dart:36:5: Error: Can't use 'super' as an expression.          
To delegate a constructor to a super constructor, put the super call as an initializer.
    super(key: key);                                                    
    ^                                                                   
lib/main.dart:34:10: Error: The setter 'title' isn't defined for the class 'MyHomePage

那是为什么?我不明白那是什么意思。 是否有关于如何在dart中定义构造函数的全面解释?我在dart.dev网站上找不到。

编辑: 将代码编辑为以下内容之后:

class MyHomePage extends StatefulWidget {
  MyHomePage(String title, String lang, {Key key}) {
    title = title;
    lang = lang.substring(0,2).toLowerCase();
    super(key: key);
  }

  final String title;
  final String lang;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

我收到来自编译器的以下投诉:

Compiler message:                                                       
lib/main.dart:36:5: Error: Can't use 'super' as an expression.          
To delegate a constructor to a super constructor, put the super call as an initializer.
    super(key: key);                                                    
    ^                                                                   
lib/main.dart:39:16: Error: Final field 'title' is not initialized.     
Try to initialize the field in the declaration or in every constructor. 
  final String title;                                                   
               ^^^^^                                                    
lib/main.dart:40:16: Error: Final field 'lang' is not initialized.      
Try to initialize the field in the declaration or in every constructor. 
  final String lang;     

1 个答案:

答案 0 :(得分:0)

在构造函数中初始化final变量有特殊的语法,请在此处回答:

https://stackoverflow.com/a/42864979/64435

使用此语法,工作代码为:

class MyHomePage extends StatefulWidget {
  MyHomePage(String title, String lang, {Key key})
      : this.title = title,
        this.lang = lang.substring(0, 2).toLowerCase(),
        super(key: key);

  final String title;
  final String lang;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}