fsw
fsw

Reputation: 3695

this.field syntactic sugar on constructor parameter list and inherited field.

Please consider following example that works totally fine:

abstract class Parent {
  String parentField;
}

class Child extends Parent {
  String childField;

  Child(parentField, childField){
    this.parentField = parentField;
    this.childField = childField;
  }
}

When i try to change child constructor to:

Child(this.parentField, this.childField);

I get:

error: line X pos Y: unresolved reference to instance field 'parentField' Child(this.parentField, this.childField);

My question is:

Is this a design decision? bug? or missing feature worth requesting?

Upvotes: 3

Views: 643

Answers (2)

Lingam
Lingam

Reputation: 602

For anyone checking after 2022, this is updated. You can now use,

Child(super.parentField, this.childField);

Upvotes: 1

Alexandre Ardhuin
Alexandre Ardhuin

Reputation: 76183

This is the expected behaviour. From the Generative Constructors section of the Dart spec.:

An initializing formal has the form this.id. It is a compile-time error if id is not the name of an instance variable of the immediately enclosing class.

Upvotes: 3

Related Questions