flutter MyhomePage({Key key, this.title}) : super(key: key); pls any one explain clearly with example flutter

我的未来我决定 提交于 2020-05-09 19:40:16

问题


In flutter anyone explain clearly with example my confusion about key, code as below

MyHomepage({Key key, this.title}) : super(key: key);

回答1:


The code is the constructor of the MyHomepage widget.

{Key key, this.title}

declares 2 optional named parameters (optional named because of {}) where

  • the first is of name key with typeKey`

  • the 2nd is of name title with the type of the field this.title and automatically initializes this.title with the passed value This is nice syntactic sugar that saves some writing.

: starts the initializer list. The initializer list allows some to execute some expressions before the call is forwarded to the constructor of the super class.

When a class is initialized, read access to this is forbidden until the call to the super constructor is completed (until the body of the constructor is executed - in your example the constructor has no body).

The initializer list is often use to validate passed parameter values with assert(key != null) or to initialize final fields with calculated values (final fields can't be initialized or updated later).

super(key: key) forwards to the constructor of the super class and passes the parameter key passed to MyHomepage to the super constructors key parameter (same as for MyHomepage({Key key})).




回答2:


Thanks for @Günter's detailed explanation which helped me at the very beginning. Here I would like to explain the background for this question, and especially the punctuations as syntax a little bit.

The mentioned line of code:

MyHomepage({Key key, this.title}) : super(key: key);

should come from the auto-generated flutter application boilerplate.

The complete context is:

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

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

For me, the weired punctuations (curly brackets{ } and two colons :) are what prevented me from comprehending its syntax.

Curly brackets of {Key key, this.title}: is the syntax for declaring optional parameters while defining function in Dart.

The 1st Colon of MyHomepage(...) : super(key: key) is a separator that specifies the initializer list (super(key: key)) of constructor function MyHomepage(...)

The 2nd Colon within super(key: key) is the way how you pass parameter to a named function ( super() in this case ).

  • For example, a function enableFlags is defined as following
void enableFlags({bool bold, bool hidden}) {...}
  • To call the function, the way Dart passes parameter to the function, is by declaring parameterName before value, seperated with colon :, which is safer for developer than pythononic way. The counterpart syntax in swift should be external parameter.
enableFlags(bold: true, hidden: false);

Wish this could help.

All the definitions and examples can be found at Dart's official document



来源:https://stackoverflow.com/questions/52056035/flutter-myhomepagekey-key-this-title-superkey-key-pls-any-one-explain

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!