Vyacheslav Dobranov
Vyacheslav Dobranov

Reputation: 5

Referencing to property from another property

I've tried but couldn't find a way to reference one property from another, like this:

class Test {
  String prop1;
  String prop2;

  Test({this.prop1, this.prop2});
}

void main() {
  var test = Test(
    prop1: 'some text',
    // and here I want to reference to prop1 but this code is erroneous:
    prop2: '$prop1',
  );

Is there any way for it (like "this" in JS)?

Upvotes: 0

Views: 74

Answers (1)

stwupton
stwupton

Reputation: 2235

You cannot reference the prop1 property before the Test object has been constructed. If you want prop1 and prop2 to be the same, create the String beforehand and use it for both of the arguments:

void main() {
  String prop = 'some text';
  var test = Test(
    prop1: prop,
    prop2: prop,
  );
}

Upvotes: 3

Related Questions