Zeeshan Tariq
Zeeshan Tariq

Reputation: 123

any dart expert explain what is the dart constructor the code below?

can anyone explain why we use the curly bracket in the argument of the constructor.

class Cars {
  String carName;
  bool isAuto;

  // create the constructor
  Cars({String honda, bool yes}) {
    carName = honda;
    isAuto = yes;
  }
}

Upvotes: 0

Views: 57

Answers (1)

David
David

Reputation: 924

Its are named parameters.

For create an instance:

Cars(honda: 'foo', yes: true);
// or 
Cars(yes: true, honda: 'foo');

If you don't use curly, will be:

class Cars {
  String carName;
  bool isAuto;

  // create the constructor
  Cars(String honda, bool yes) {
    carName = honda;
    isAuto = yes;
  }
}

And then you will create a new instance by order:

Cars('foo', true);

Also, you can initialize automatically:

class Cars {
  String carName;
  bool isAuto;

  Cars(this.carName, this.isAuto);
}

Upvotes: 3

Related Questions