Manfred Kraus
Manfred Kraus

Reputation: 1

Dart: base class variable initialisation

How can a variable of the base class gets an initial value in dart? I searched the documentation and experimented with :super a: , A(a:..) and such things but could not find a solution that is accepted by the compiler.

var myobject = B (a:'some text', b:'more text'); // this leads to: The named parameter 'a' isn't defined.

class A {
  String a;
  A({this.a});
}

class B extends A {
  String b;
  B({this.b});
}

Upvotes: 0

Views: 89

Answers (1)

Stephen
Stephen

Reputation: 4249

You want to add a to the arguments of b's constructor and use it to call the super constructor like so:

class A {
  String a;
  A({this.a});
}

class B extends A {
  String b;
  B({aa, this.b}) : super(a: aa);
}

Constructor documentation goes over this concept.

Upvotes: 1

Related Questions