laike9m laike9m - 1 month ago 9x
Dart Question

Dart constructor initialization order

class Base {
num b;
Base() {
print("in Base");

Base.initB(b) {
b = b;
print("in Base.initB");

class Point extends Base {
num x;
num y;
final num f;

Point(this.x, int y, fuck, b): f=fuck, super.initB(b) {
this.y = y;

main() {
var a = new Point(1, 2, 3, 4);
// output:
// in Base initB
// 1

I'm learning Dart but found the documentation obscure. I want to know the initialization order of instance variables. There are two questions:

  1. From my understanding, the initialization order is
    x, f -> b -> y
    , is it correct?

  2. x
    , which is first?


I don't think there is an order defined between x and f and if it were, it wouldn't matter. You can't access x or f from anywhere until after suber.initB(b) completed and the constructor body of Point started to be executed.

Besides that the order you derived is correct.