@@ -34,3 +34,72 @@ class Point {
34
34
// #docregion idiomatic-constructor
35
35
}
36
36
// #enddocregion idiomatic-constructor
37
+
38
+ // #docregion initialize-declaration
39
+ class PointA {
40
+ double x = 1.0 ;
41
+ double y = 2.0 ;
42
+
43
+ // The parameterless constructor is not even be needed to set to (1.0,2.0)
44
+ PointA ();
45
+
46
+ @override
47
+ String toString () {
48
+ return 'PointA($x ,$y )' ;
49
+ }
50
+ }
51
+ // #enddocregion initialize-declaration
52
+
53
+ // #docregion initialize-formal
54
+ class PointB {
55
+ final double x;
56
+ final double y;
57
+
58
+ // Sets the x and y instance variables
59
+ // before the constructor body runs.
60
+ PointB (this .x, this .y);
61
+
62
+ // Initializing formal parameters can also be optional.
63
+ PointB .optional ([this .x = 0.0 , this .y = 0.0 ]);
64
+ PointB .named ({required this .x, required this .y});
65
+
66
+ // Private fields cannot be used as named initializing formals.
67
+ PointB .namedPrivate ({required double x, required double y})
68
+ : _x = x,
69
+ _y = y;
70
+ }
71
+ // #enddocregion initialize-formal
72
+
73
+ // #docregion initialize-named
74
+ class PointC {
75
+ double x; // must be set in constructor
76
+ double y; // must be set in constructor
77
+
78
+ // Generative constructor with initializing formal parameters
79
+ // with default values
80
+ PointC ({this .x = 1.0 , this .y = 1.0 });
81
+
82
+ @override
83
+ String toString () {
84
+ return 'PointC($x ,$y )' ;
85
+ }
86
+ }
87
+
88
+ // Constructor using named variables.
89
+ final pointC = PointC (x: 2.0 , y: 2.0 );
90
+ // #enddocregion initialize-named
91
+
92
+ // #docregion initialize-null
93
+ class PointD {
94
+ double ? x; // null if not set in constructor
95
+ double ? y; // null if not set in constructor
96
+
97
+ // Generative constructor with initializing formal parameters
98
+ PointD (this .x, this .y);
99
+
100
+ @override
101
+ String toString () {
102
+ return 'PointD($x ,$y )' ;
103
+ }
104
+ }
105
+ // #enddocregion initialize-null
0 commit comments