【问题标题】:What does ClassName method() => ClassName() mean in Dart?Dart 中的 ClassName method() => ClassName() 是什么意思?
【发布时间】:2019-06-22 06:57:00
【问题描述】:

在 Flutter 项目中,我看到了这个 sn-p:_State createState() => _State();
ClassName method() => ClassName() 在 Dart 中是什么意思,createState() 方法在哪里定义了所有这些对 Flutter 意味着什么? 完整代码如下:

class Nearby extends StatefulWidget {
  @override
  _State createState() => _State();
}

class _State extends State<Nearby> {
  GoogleMapController mapController;
  LatLng _center;

  @override
  void initState() {
    super.initState();
    _getCurrentLocation();
  }


  @override
  Widget build(BuildContext context) {
    return Container()
}}

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    来自Dart language tour

    =&gt; expr 语法是{ return expr; } 的简写。 =&gt; 符号有时被称为箭头语法。

    因此:

    _State createState() => _State();
    

    表示createState() 是一个调用_State 构造函数(不带参数)并返回新构造的_State 对象的函数。

    至于对于 Flutter 的意义,请看StatefulWidget.createState documentation

    在树中的给定位置为此小部件创建可变状态。

    子类应重写此方法以返回与其关联的 State 子类的新创建实例

    (您可能还想查看StatefulWidget documentation。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-08
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      • 1970-01-01
      • 2012-10-06
      相关资源
      最近更新 更多