不明点
以下のコードのうち MyHomePage({Key key, this.title}) : super(key: key);
の理解に躓いてしまいました。
Dart
1class MyHomePage extends StatefulWidget { 2 MyHomePage({Key key, this.title}) : super(key: key); 3 final String title; 4 5 6 _MyHomePageState createState() => _MyHomePageState(); 7}
公式ドキュメンテーションによれば、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
1//a Tour of Dart Language #redirect part 2class Point { 3 double x, y; 4 5 // The main constructor for this class. 6 Point(this.x, this.y); 7 8 // Delegates to the main constructor. 9 Point.alongXAxis(double x) : this(x, 0); 10}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/06 07:15
2022/05/06 07:49
2022/05/06 13:29