如何解决以下方法如何使用为构造函数保留的“语法糖”?
为说明我的观点,以下代码包含一个名为ColorValueChanger的方法,该方法使用this.passedIn作为可选参数。我以为这是为构造函数保留的?
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,);
}
}
解决方法
它是一个构造函数。 听起来更像是示例中的错字。
固定代码为:
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,);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。