【问题标题】:How is it possible that the following method uses "syntactic sugar" reserved for constructors?以下方法怎么可能使用为构造函数保留的“语法糖”?
【发布时间】:2020-08-13 22:02:06
【问题描述】:

为了说明我的观点,以下代码包含一个名为 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 flutter oop dart constructor


    【解决方案1】:

    它是一个构造函数。 听起来更像是示例中的错字。

    固定的代码是:

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

    【讨论】:

      猜你喜欢
      • 2020-12-14
      • 1970-01-01
      • 2019-01-11
      • 2018-06-30
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多