What is a Dart factory constructor and common use cases?
When Dart factories beat generative constructors.
Explain a factory need not create new instances and can return cached objects or subtypes; contrast with generative ones; give JSON or singleton examples.
WHAT THIS TESTS: This question probes whether you understand the semantic difference between generative and factory constructors in Dart, and whether you can identify real scenarios where a factory is the only appropriate tool. Interviewers care that you know a factory constructor does not automatically create a new instance, and that you can articulate why that matters for caching, subtyping, or controlled instantiation.
A GOOD ANSWER COVERS: First, the core definition: a factory constructor uses the factory keyword and is not required to return a new instance of the enclosing class each time it is called. Second, the explicit return requirement: unlike a generative constructor, a factory must explicitly return an object and cannot rely on implicit allocation. Third, the contrast with generative constructors: generative constructors always produce a fresh instance of the exact class being constructed, while a factory can return an existing cached instance, an instance of a subtype, or even null in legacy code. Fourth, concrete use cases: JSON parsing where you might maintain an identity map to reuse objects with the same ID, or a singleton pattern where the factory returns the same instance on every call.
COMMON WRONG ANSWERS: Candidates often confuse a factory constructor with a static method, claiming the difference is purely syntactic; the key distinction is that a factory constructor is invoked with new or as a normal constructor call and participates in the class constructor namespace. Another red flag is asserting that factory constructors improve performance or memory allocation speed; they are a semantic tool, not an optimization. Saying that factory constructors are only for named constructors is also incorrect, since unnamed factories exist. Finally, forgetting that a factory must explicitly return an instance shows a lack of hands-on Dart experience.
LIKELY FOLLOW-UPS: An interviewer might ask how factory constructors interact with const constructors, since a factory cannot be const unless it redirects to a const generative constructor. They might also ask you to compare a factory constructor with a static factory method, probing why you would choose one over the other; the answer is that the factory constructor preserves the constructor call syntax and allows subclassing without changing call sites. Another follow-up is how to handle JSON deserialization with nested objects or generic types using factory constructors.
ONE CONCRETE EXAMPLE: Imagine a User class with a factory constructor named fromJson that takes a map. Inside the factory, you check a static private cache map using the ID from the JSON. If the ID exists, you return the cached User. If not, you create a new User via a private generative constructor and store it in the cache before returning it. This prevents duplicate User objects for the same ID across your app. For a singleton, you keep a static private instance variable and return it if it exists, otherwise create it with the private constructor and assign it before returning.
Read the original → dart.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.