Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colon : in Dart constructor syntax

Tags:

flutter

dart

class X extends Y {
  X(int a, int b) : super(a,b);
}

Can someone give me an explanation about the syntax meaning of the colon :?

like image 272
Little Monkey Avatar asked May 08 '19 08:05

Little Monkey


People also ask

What does Colon do in Dart?

It is a , -separated list of expressions that can access constructor parameters and can assign to instance fields, even final instance fields. This is handy to initialize final fields with calculated values.

How do you define a constructor in darts?

Dart defines a constructor with the same name as that of the class. A constructor is a function and hence can be parameterized. However, unlike a function, constructors cannot have a return type. If you don't declare a constructor, a default no-argument constructor is provided for you.

What is the syntactic sugar concept in Dart constructor?

In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.


2 Answers

This feature in Dart is called "initializer list".
It allows you to initialize fields of your class, make assertions and call the super constructor.

This means that it is not the same as the constructor body. As I said, you can only initialize variables and only access static members. You cannot call any (non-static) methods.

The benefit is that you can also initialize final variables, which you cannot do in the constructor body. You also have access to all parameters that are passed to the constructor, which you do not have when initializing the parameters directly in the parentheses.
Additionally, you can use class fields on the left-hand of an assignment with the same name as a parameter on the right-hand side that refers to a parameter. Dart will automatically use the class field on the left-hand side.
Here is an example:

class X {
  final int number;

  X(number) : number = number ?? 0;
}

The code above assigns the parameter named number to the final field this.number if it is non-null and otherwise it assigns 0. This means that the left-hand number of the assignment actually refers to this.number. Now, you can even make an assertion that will never fail (and is redundant because of that, but I want to explain how everything works together):

class X {
  final int number;

  X(number): number = number ?? 0, assert(number != null);
}

Learn more.

like image 196
creativecreatorormaybenot Avatar answered Sep 26 '22 15:09

creativecreatorormaybenot


It's ok to access non static member in initializer list.

class Point {
  num x, y;

  Point(this.x, this.y);
  Point.origin(): this.x = 10, this.y = 10;
}

main() {
  Point p = Point.origin();
  print(p.x); // 10
}
like image 21
Bo Ding Avatar answered Sep 23 '22 15:09

Bo Ding