@@ -92,8 +92,8 @@ Before the constructor body (if any),
92
92
specify the superclass constructor after a colon (` : ` ).
93
93
94
94
In the following example,
95
- the constructor for the ` Employee ` class calls the named constructor
96
- for its superclass, ` Person ` . Click ** Run ** to execute the code.
95
+ the ` Employee ` class constructor calls the named constructor
96
+ for its superclass, ` Person ` . To execute the following code, click ** Run ** .
97
97
98
98
<? code-excerpt "employee.dart (super)" plaster="none"?>
99
99
``` dart:run-dartpad:height-450px:ga_id-non_default_superclass_constructor
@@ -187,8 +187,9 @@ class Vector2d {
187
187
class Vector3d extends Vector2d {
188
188
final double z;
189
189
190
- // Conflicts with the super constructor Vector2d(this.x, this.y)
191
- Vector3d(super.y, this.z);
190
+ // Forward the x and y parameters to the default super constructor like:
191
+ // Vector3d(final double x, final double y, this.z) : super(x, y);
192
+ Vector3d(super.x, super.y, this.z);
192
193
}
193
194
```
194
195
@@ -215,12 +216,11 @@ and named arguments to the super constructor invocation
215
216
``` dart
216
217
class Vector2d {
217
218
// ...
218
-
219
219
Vector2d.named({required this.x, required this.y});
220
220
}
221
221
222
222
class Vector3d extends Vector2d {
223
- // ...
223
+ final double z;
224
224
225
225
// Forward the y parameter to the named super constructor like:
226
226
// Vector3d.yzPlane({required double y, required this.z})
@@ -229,6 +229,27 @@ class Vector3d extends Vector2d {
229
229
}
230
230
```
231
231
232
+ ### Constant constructors
233
+
234
+ If your class produces unchanging objects, make these
235
+ objects compile-time constants.
236
+ To make objects compile-time constants, define a ` const ` constructor
237
+ with all instance variables set as ` final ` .
238
+
239
+ <? code-excerpt "immutable_point.dart"?>
240
+ ``` dart
241
+ class ImmutablePoint {
242
+ static const ImmutablePoint origin = ImmutablePoint(0, 0);
243
+
244
+ final double x, y;
245
+
246
+ const ImmutablePoint(this.x, this.y);
247
+ }
248
+ ```
249
+
250
+ Constant constructors don't always create constants.
251
+ To learn more, consult the section on [ using constructors] [ ] .
252
+
232
253
### Redirecting constructors
233
254
234
255
A constructor might redirect to another constructor in the same class.
@@ -248,26 +269,6 @@ class Point {
248
269
}
249
270
```
250
271
251
- ### Constant constructors
252
-
253
- If your class produces objects that never change, you can make these
254
- objects compile-time constants. To do this, define a ` const ` constructor
255
- and make sure that all instance variables are ` final ` .
256
-
257
- <? code-excerpt "immutable_point.dart"?>
258
- ``` dart
259
- class ImmutablePoint {
260
- static const ImmutablePoint origin = ImmutablePoint(0, 0);
261
-
262
- final double x, y;
263
-
264
- const ImmutablePoint(this.x, this.y);
265
- }
266
- ```
267
-
268
- Constant constructors don't always create constants.
269
- To learn more, consult the section on [ using constructors] [ ] .
270
-
271
272
### Factory constructors
272
273
273
274
When encountering one of two cases of implementing a constructor,
@@ -336,6 +337,25 @@ var logMap = {'name': 'UI'};
336
337
var loggerJson = Logger.fromJson(logMap);
337
338
```
338
339
340
+ ### Redirecting factory constructors
341
+
342
+ A redirecting factory constructor specifies a call to a constructor of another
343
+ class to use whenever someone makes a call to the redirecting constructor.
344
+
345
+ ``` dart
346
+ factory Listenable.merge(List<Listenable> listenables) = _MergingListenable
347
+ ```
348
+
349
+ It might appear that ordinary factory constructors
350
+ could create and return instances of other classes.
351
+ This would make redirecting factories unnecessary.
352
+ Redirecting factories have several advantages:
353
+
354
+ * An abstract class might provide a constant constructor
355
+ that uses the constant constructor of another class.
356
+ * A redirecting factory constructor avoids the need for forwarders
357
+ to repeat the formal parameters and their default values.
358
+
339
359
## Parameter initialization
340
360
341
361
Dart can initialize parameters in a constructor in three ways.
@@ -347,8 +367,8 @@ Initialize the constructor parameters when you declare variables.
347
367
<? code-excerpt "point_alt.dart (initialize-declaration)" plaster="none"?>
348
368
``` dart
349
369
class PointA {
350
- [! double x = 1.0;!]
351
- [! double y = 2.0;!]
370
+ double x = 1.0;
371
+ double y = 2.0;
352
372
353
373
// The parameterless constructor is not even be needed to set to (1.0,2.0)
354
374
PointA();
@@ -415,7 +435,7 @@ class PointC {
415
435
416
436
// Generative constructor with initializing formal parameters
417
437
// with default values
418
- [! PointC({this.x = 1.0, this.y = 1.0});!]
438
+ PointC({this.x = 1.0, this.y = 1.0});
419
439
420
440
@override
421
441
String toString() {
@@ -424,7 +444,7 @@ class PointC {
424
444
}
425
445
426
446
// Constructor using named variables.
427
- [! final pointC = PointC(x: 2.0, y: 2.0);!]
447
+ final pointC = PointC(x: 2.0, y: 2.0);
428
448
```
429
449
430
450
All variables introduced from initializing formal parameters are both
@@ -440,8 +460,8 @@ The constructor parameters could be set as nullable and not be initialized.
440
460
<? code-excerpt "point_alt.dart (initialize-null)" plaster="none"?>
441
461
``` dart
442
462
class PointD {
443
- [! double? x;!] // null if not set in constructor
444
- [! double? y;!] // null if not set in constructor
463
+ double? x; // null if not set in constructor
464
+ double? y; // null if not set in constructor
445
465
446
466
// Generative constructor with initializing formal parameters
447
467
PointD(this.x, this.y);
0 commit comments