Skip to content

Commit 84c218b

Browse files
authored
Refactor Constructors page (#5757)
Fixes #2009 Fixes #2221 Fixes #5227 Fixes #5506 Fixes #5728 Fixes #5738 Fixes #5739
1 parent 6c3ed12 commit 84c218b

File tree

6 files changed

+492
-232
lines changed

6 files changed

+492
-232
lines changed

examples/misc/lib/language_tour/classes/point_alt.dart

+66-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
///
77
// #docregion idiomatic-constructor
88
class Point {
9-
double x = 0;
10-
double y = 0;
9+
// Initializer list of variables and values
10+
double x = 2.0;
11+
double y = 2.0;
1112

1213
// Generative constructor with initializing formal parameters:
1314
Point(this.x, this.y);
@@ -34,3 +35,66 @@ class Point {
3435
// #docregion idiomatic-constructor
3536
}
3637
// #enddocregion idiomatic-constructor
38+
39+
// #docregion initialize-declaration
40+
class PointA {
41+
double x = 1.0;
42+
double y = 2.0;
43+
44+
// The implicit default constructor sets these variables to (1.0,2.0)
45+
// PointA();
46+
47+
@override
48+
String toString() {
49+
return 'PointA($x,$y)';
50+
}
51+
}
52+
// #enddocregion initialize-declaration
53+
54+
// #docregion initialize-formal
55+
class PointB {
56+
final double x;
57+
final double y;
58+
59+
// Sets the x and y instance variables
60+
// before the constructor body runs.
61+
PointB(this.x, this.y);
62+
63+
// Initializing formal parameters can also be optional.
64+
PointB.optional([this.x = 0.0, this.y = 0.0]);
65+
}
66+
// #enddocregion initialize-formal
67+
68+
// #docregion initialize-named
69+
class PointC {
70+
double x; // must be set in constructor
71+
double y; // must be set in constructor
72+
73+
// Generative constructor with initializing formal parameters
74+
// with default values
75+
PointC.named({this.x = 1.0, this.y = 1.0});
76+
77+
@override
78+
String toString() {
79+
return 'PointC.named($x,$y)';
80+
}
81+
}
82+
83+
// Constructor using named variables.
84+
final pointC = PointC.named(x: 2.0, y: 2.0);
85+
// #enddocregion initialize-named
86+
87+
// #docregion initialize-null
88+
class PointD {
89+
double? x; // null if not set in constructor
90+
double? y; // null if not set in constructor
91+
92+
// Generative constructor with initializing formal parameters
93+
PointD(this.x, this.y);
94+
95+
@override
96+
String toString() {
97+
return 'PointD($x,$y)';
98+
}
99+
}
100+
// #enddocregion initialize-null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// #docregion named
2+
class Vector2d {
3+
// #enddocregion named
4+
final double x;
5+
final double y;
6+
7+
Vector2d(this.x, this.y);
8+
// #docregion named
9+
// ...
10+
Vector2d.named({required this.x, required this.y});
11+
}
12+
13+
class Vector3d extends Vector2d {
14+
final double z;
15+
16+
// Forward the y parameter to the named super constructor like:
17+
// Vector3d.yzPlane({required double y, required this.z})
18+
// : super.named(x: 0, y: y);
19+
Vector3d.yzPlane({required super.y, required this.z}) : super.named(x: 0);
20+
}
21+
// #enddocregion named
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// #docregion positional
2+
class Vector2d {
3+
final double x;
4+
final double y;
5+
6+
Vector2d(this.x, this.y);
7+
}
8+
9+
class Vector3d extends Vector2d {
10+
final double z;
11+
12+
// Forward the x and y parameters to the default super constructor like:
13+
// Vector3d(final double x, final double y, this.z) : super(x, y);
14+
Vector3d(super.x, super.y, this.z);
15+
}
16+
// #enddocregion positional

src/content/guides/whats-new.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ we made the following changes to this site:
560560
[Low-level HTML tutorials]: /web/get-started
561561

562562
[native types]: /interop/c-interop#interfacing-with-native-types
563-
[initializing formal parameters]: /language/constructors#initializing-formal-parameters
563+
[initializing formal parameters]: /language/constructors#use-initializing-formal-parameters
564564
[support for packages]: /tools/dartpad#library-support
565565
[asynchronous programming codelab]: /codelabs/async-await
566566
[why asynchronous code matters]: /codelabs/async-await#why-asynchronous-code-matters

src/content/language/classes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ can pass a static method as a parameter to a constant constructor.
369369
[`Type`]: {{site.dart-api}}/{{site.sdkInfo.channel}}/dart-core/Type-class.html
370370
[type test operator]: /language/operators#type-test-operators
371371
[Getters and setters]: /language/methods#getters-and-setters
372-
[initializer list]: /language/constructors#initializer-list
372+
[initializer list]: /language/constructors#use-an-initializer-list
373373
[factory constructor]: /language/constructors#factory-constructors
374374
[late-final-ivar]: /effective-dart/design#avoid-public-late-final-fields-without-initializers
375375
[nullable type]: /null-safety/understanding-null-safety#using-nullable-types

0 commit comments

Comments
 (0)