Factory
In Dart, factory constructors provide an alternative to the conventional constructors and function differently. Unlike " normal" constructors, factory constructors cannot be private and must have a body. The creation process for factory constructors is designed to throw exceptions when developers provide invalid data.
Now let's explore how to create such factory constructors:
The primary entry point for creation is the FactorySpec class and its companion object. Most of the functions that can be used are equal to the function stack from normal constructors.
For our example we say that we have a class called Singleton and there is already a private constructor _internal in.
Now we want an additional factory constructor that returns the instance of the private constructor.
val singletonClass = ClassName("Singleton")
val factoryConstructor = FactorySpec.builder(singletonClass)
.addCode("return $T._internal();", singletonClass)
.build()factory Singleton() {
return Singleton._internal();
}There are more variants for factory constructors, which are supported by the API. To change the behaviour of the generation you need to update the constructor delegation over the delegation() function.
The constructor delegation is an enum which contains all possible delegation types. By default, the value is set to NONE which indicates that the generation process doesn't use any delegation.
The enum provides the following delegation options LAMBDA and REDIRECT. The LAMBDA delegation forces the generation to generate the body of the factory constructor as a lambda expression. The last option REDIRECT triggers a generation which generates the constructor which redirects to another constructor. That option is the only option to use any kind of redirects for such constructors.
The following snippet shows an example for the usage of the delegation
We enhance the previous example with the delegation option LAMBDA:
val singletonClass = ClassName("Singleton")
val factoryConstructor = FactorySpec.builder(singletonClass)
.delegation(ConstructorDelegation.LAMBDA)
.addCode("return $T._internal();", singletonClass)
.build()factory Singleton() => Singleton._internal();Last updated