Home > Software design >  Why to use this specific type of constructor in Dart?
Why to use this specific type of constructor in Dart?

Time:12-07

I am starting to learn dart using their own documentation from here : constructors

I came across this type of contractor :

Vector2d.named({required this.x, required this.y});

They have not specified what this does or how it works. they only showed this in a small snippet with no clarification. Can someone please elaborate how this contractor works and what is the difference between a normal C-styled constructor and this one?

I tried googling about this style of constructor but couldn't get much because of the special characters in the syntax.

CodePudding user response:

You can head to DartPad and paste the code below to try it.

class Vector2d {
  final double x;
  final double y;

  Vector2d(this.x, this.y);
  Vector2d.named({required this.x, required this.y});
}

main() {
  var v1 = Vector2d(1, 2);
  var v2 = Vector2d.named(x:3, y:4);
  var v3 = Vector2d.named(y:5, x:6);
}

v1 is created using a "normal" constructor: first parameter will be assigned to x, the second to y.

v2 is created using a so called named constructor, where you have to specify which variable will get which value. The order here is irrelevant, see v3. Named constructors are also handy when you have to deal with a large number of parameters.

The required keyword means it is a required parameter. If you remove the required keyword, you will get an error that x and y can't have a value of null, because you no longer forced to specify x and y, they can be null. To fix this you can make the type nullable by adding a ? after the type:

class Vector2d {
  final double? x;
  final double? y;

  Vector2d([this.x, this.y]);
  Vector2d.named({this.x, this.y});
}

main() {
  var v4 = Vector2d(1);
  var v5 = Vector2d.named(x:5);
  var v6 = Vector2d.named(y:6);
}

This also makes x and y named optional parameters: you are not forced to initialise them, but keep in mind that you need to check them before using them (because they're null). v5 and v6 are created with named constructor, and one of their member is null. Try and see what happens when you print out v5.x and v5.y.

You can also have positional optional parameters. Notice the [] in the first constructor. v4 is created with positional optional constructor. This means that you can have zero, one or two positional arguments when you use that constructor. See what happens when you print out v4.x and v4.y.

And lastly: you can mix these named and positioned optional parameters with required ones:

class Mixed {
  final int? x;
  final int? y;
  final int? z;
  
  Mixed.namedOptional(this.x, {required this.y, this.z});
  Mixed.positionalOptional(this.x, [this.y, this.z]);
}

And note that these rules apply to any regular functions, not just constructors!

I suggest to watch this video to understand null and null safety in dart and see this and this other answers related to the topic

  • Related