dart advantage of a factory constructor identifier
A normal constructor always returns a new instance of the current class (except when the constructor throws an exception).
A factory constructor is quite similar to a static method with the differences that it
- can only return an instance of the current class or one of its subclasses
- can be invoked with
new
but that is now less relevant sincenew
became optional. - has no initializer list (no
: super()
)
So a factory constructor can be used
- to create instances of subclasses (for example depending on the passed parameter
- to return a cached instance instead of a new one
- to prepare calculated values to forward them as parameters to a normal constructor so that final fields can be initialized with them. This is often used to work around limitations of what can be done in an initializer list of a normal constructor (like error handling).
In your example this code
studentId: parsedJson['id'],
studentName : parsedJson['name'],
studentScores : parsedJson ['score']
could be moved to the body of a normal constructor because no final
fields need to be initialized.
A factory constructor vs. a normal constructor
- A factory constructor invokes another constructor.
- Since a factory constructor does not directly create a new instance, it cannot use a constructor initializer list.
- A normal constructor always returns a new instance of the class. A factory constructor is permitted to return an existing instance, an instance of a derived class, or
null
. (However, some people dislike returningnull
from a factory constructor. Note that returningnull
from a factory constructor is disallowed with null-safety.) - Due to the above, an extending class cannot invoke a factory constructor as the superclass constructor. A class that provides only
factory
constructors therefore cannot be extended with derived classes.
A factory constructor vs. a static method
- A factory constructor can be the unnamed, default constructor of a class.
- A factory constructor can be used with
new
. (But usingnew
is now discouraged.) - Until Dart 2.15, constructors could not be used as tear-offs (i.e., they can be used as callbacks), whereas
static
methods could. - Static methods can be
async
. (A factory constructor must return a type of its class, so it cannot return aFuture
.) - Factory constructors can be declared
const
. - In null-safe Dart, a factory constructor cannot return a nullable type.
- In generated dartdoc documentation, a factory constructor obviously will be listed in the "Constructors" section (which is prominently at the top) whereas a static method will be in the "Static Methods" section (which currently is buried at the bottom).
After I've been noticing and wondering the same, and given I don't think the other answers actually answer the question ("I've been investigating JSON parsing [...] I'm trying to understand the advantage of using a factory constructor verses a plain constructor"), here my try:
there's no advantage or difference that I could see or understand, when parsing json, in using a factory constructor instead of a plain constructor. I tried both, and both works fine, with all the types of parameters. I decided eventually to adopt the factory constructor, because of the convenience of how the code is written, and readability, but it's a matter of choice and both will work fine in all the cases.