简体   繁体   English

以下方法怎么可能使用为构造函数保留的“语法糖”?

[英]How is it possible that the following method uses “syntactic sugar” reserved for constructors?

To illustrate my point, the following code contains a method called ColorValueChanger uses this.passedIn as an optional parameter.为了说明我的观点,以下代码包含一个名为 ColorValueChanger 的方法,它使用 this.passedIn 作为可选参数。 I thought this was reserved for constructors?我以为这是为构造函数保留的?

class Foo extends StatefulWidget {
 final String passedIn;
 // Value passed in from its host
 ColorValueChanger({Key key, this.passedIn}) : super(key: key);
 _FooState createState() => new _FooState();
}
class _FooState extends State<Foo> {
 @override
 Widget build(BuildContext context) {
 return Text(widget.passedIn,);
 }
}

It is a constructor.它是一个构造函数。 It sounds more like there is a typo in the example.这听起来更像是示例中的错字。

The fixed code would be:固定代码是:

class Foo extends StatefulWidget {
 final String passedIn;
 // Value passed in from its host
 Foo({Key key, this.passedIn}) : super(key: key);
 _FooState createState() => new _FooState();
}
class _FooState extends State<Foo> {
 @override
 Widget build(BuildContext context) {
 return Text(widget.passedIn,);
 }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM