What do curly braces wrapping constructor argument

2020-02-26 03:23发布

问题:

Consider the following piece of code:

class Person {
  String id;
  String name;
  ConnectionFactory connectionFactory;

  // What is this constructor doing?
  Person({this.connectionFactory: _newDBConnection});

}

If you precede a constructor's argument with this, the corresponding field will be automatically initialized, but why {...}?

回答1:

This makes the argument a named optional argument.

When you instantiate a Person you can

Person p;
p = new Person(); // default is _newDbConnection
p = new Person(connectionFactory: aConnectionFactoryInstance);
  • without {} the argument would be mandatory
  • with [] the argument would be an optional positional argument
// Constructor with positional optional argument
Person([this.connectionFactory = _newDBconnection]);
...
Person p;
p = new Person(); // same as above
p = new Person(aConnectionFactoryInstance); // you don't specify the parameter name

Named optional parameters are very convenient for boolean arguments (but of course for other cases too).

p = new Person(isAlive: true, isAdult: false, hasCar: false); 

There is a specific order in which these argument types can be used:

  1. mandatory (positional) arguments (only positional arguments can be mandatory)
  2. optional positional arguments
  3. (optional) named arguments (named arguments are always optional)

Note that positional and named optional arguments use a different delimiter for the default value. The named requires : but the positional requires =. The language designers argue that the colon fits better with the Map literal syntax (I would at least have used the same delimiter for both).

= is supported as delimiter since Dart 2 and preferred according to the style guide while : is still supporzed.

See also:

  • What is the difference between named and optional parameters in Dart?
  • Functions Are Fun, Pt 1 - Dart Tips, Ep 6
  • Chapter 2. A Tour of the Dart Language - Functions
  • Chapter 2. A Tour of the Dart Language - Constructors


回答2:

the this. connectionFactory in

Person({this.connectionFactory: _newDBConnection});

is called Automatic Class Member Variable Initialization. See this example



标签: dart