I've been investigating JSON parsing for my Flutter app and have a question about factory constructors that I can't resolve. I'm trying to understand the advantage of using a factory constructor versus a plain constructor. For example, I see quite a few JSON parsing examples that create a model class with a JSON constructor like this:
class Student{ String studentId; String studentName; int studentScores; Student({ this.studentId, this.studentName, this.studentScores }); factory Student.fromJson(Map<String, dynamic> parsedJson){ return Student( studentId: parsedJson['id'], studentName : parsedJson['name'], studentScores : parsedJson ['score'] ); } }
I've also seen an equal number of examples that DON'T declare the constructor as a factory. Both types of classname.fromJSON constructors create an object from the JSON data so is there an advantage to declaring the constructor as a factory or is using a factory here superfluous?
A factory constructor is a constructor that can be used when you don't necessarily want a constructor to create a new instance of your class. This might be useful if you hold instances of your class in memory and don't want to create a new one each time (or if the operation of creating an instance is costly).
Overview. In Dart, we use the factory keyword to identify a default or named constructor. We use the factory keyword to implement constructors that do not produce new instances of an existing class.
A constructor is a special function of the class that is responsible for initializing the variables of the class. Dart defines a constructor with the same name as that of the class. A constructor is a function and hence can be parameterized. However, unlike a function, constructors cannot have a return type.
Factory Method is referred as a creational design pattern which provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. Also known as virtual constructors. Lets clear it with a small code.
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
new
but that is now less relevant since new
became optional.: super()
) So a factory constructor can be used
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.
In the particular example in the question, there's no advantage to using a factory
constructor. It makes no difference to callers (there is no expectation to receive an already-existing object), and this particular factory
constructor could have been a normal constructor that delegated to the main constructor instead.
In general, the factory
keyword is not very useful and provides an advantage only in special circumstances.
null
. (However, some people dislike returning null
from a factory constructor. Note that returning null
from a factory constructor is disallowed with null-safety.)factory
constructors therefore cannot be extended with derived classes.new
. (But using new
is now discouraged.)static
methods could.async
. (A factory constructor must return a type of its class, so it cannot return a Future
.)const
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With