不明点
以下のコードのうち MyHomePage({Key key, this.title}) : super(key: key);
の理解に躓いてしまいました。
Dart
class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; _MyHomePageState createState() => _MyHomePageState(); }
公式ドキュメンテーションによれば、superに関しては、以下の通りの記載があり
If the superclass doesn’t have an unnamed, no-argument constructor, then you must manually call one of the constructors in the superclass. Specify the superclass constructor after a colon (:), just before the constructor body (if any).
スーパークラスのコンストラクタの手動による呼び出しについて触れられております。(逆にsuper
に関する説明はその程度しか記載がないように思えます)
実際に MyHomePage({Key key, this.title}) : super(key: key);
を観察してみると、公式にあるようなsuper
の使い方とは異なり、スーパークラスのコンストラクタの手動での呼び出しとは異なるようにも思えます。
一方、オリジナルコード出典もとのQiita記事によれば、このsuper
を含めた MyHomePage({Key key, this.title}) : super(key: key);
はリダイレクトコンストラクタであると記載がありました。ただ、公式ドキュメンテーションにはリダイレクトコンストラクタの文脈におけるsuper
の利用については何も触れられておりません。
ご質問
これらを踏まえて、 MyHomePage({Key key, this.title}) : super(key: key);
はどのように理解すればよいのでしょうか?
Qiita記事ではこれはリダイレクトコンストラクタで親にもkeyを渡していると言うような説明をざっくりされていますが、特に公式のリダイレクトコンストラクタには:
が2度も出てこないため、混乱しております。。
Dart
//a Tour of Dart Language #redirect part class Point { double x, y; // The main constructor for this class. Point(this.x, this.y); // Delegates to the main constructor. Point.alongXAxis(double x) : this(x, 0); }
まだ回答がついていません
会員登録して回答してみよう