简体   繁体   中英

What does this class-const-Syntax prefixed by Classname mean in Dart?

Although I thought being familiar with programming language Dart, i stumbled upon this syntax in an example for Bloc:

class AuthenticationState extends Equatable {
  const AuthenticationState._({
    this.status = AuthenticationStatus.unknown,
    this.user = User.empty,
  });

  const AuthenticationState.unknown() : this._();

  const AuthenticationState.authenticated(User user)
      : this._(status: AuthenticationStatus.authenticated, user: user);

  const AuthenticationState.unauthenticated()
      : this._(status: AuthenticationStatus.unauthenticated);

  final AuthenticationStatus status;
  final User user;

  @override
  List<Object> get props => [status, user];
}

I know how to define a class constant and a const constructor.

However, why is the classname prefixed here everywhere?

const AuthenticationState._({
    this.status = AuthenticationStatus.unknown,
    this.user = User.empty,
  });

That's a named constructor. In dart you can define constructors in two ways, either using ClassName or ClassName.someOtherName .

Eg: Consider you have a class called person with 2 variables, name and carNumber. Everyone has a name but carNumber is not necessary. In that situation, if you implement the default constructor, you have to initialise it like:

Person("Name", "");

So if you want to add some syntactic sugar, you can add a named constructor like below:

class Person {
  String name;
  String carNumber;

  // Constructor creates a person with name & car number
  Person(this.name, this.carNumber);

  // Named constructor that only takes name and sets carNumber to ""
  Person.withOutCar(String name): this(name, "");

  // Named constructor with no arguments, just assigns "" to variables
  Person.unknown(): this("", "");
}

And you can initialise the object like:

Person.withOutCar("Name");

In the above example the named constructors are being redirected to your actual constructor with pre-defined default values.

You can read more about named constructors here: Dart Language Tour

As the above answer was not really clear/complete to me, when I stumbled upon this, please me let me know, whether my understanding is correct:

const AuthenticationState._({
    this.status = AuthenticationStatus.unknown,
    this.user = User.empty,
  });

The constructor using ._() is private to the library, I guess this is a security feature, so you can create an instance only from within the library.

const AuthenticationState.unknown() : this._();

If the object is created as .unknown , the default constructor from above is called (as private), such using the default values. In the other 2 cases, one or both default values are replaced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM