📱 Flutter Intermediate

What is a factory constructor in Dart?

Why Interviewers Ask This

This question targets practical, hands-on experience with Flutter. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.

Answer

A factory constructor is a constructor that does not always create a new instance of the class. Instead, it can return an existing instance (singleton pattern) or a subtype. It is declared with the factory keyword. The most common use is JSON deserialization: factory User.fromJson(Map<String, dynamic> json) => User(name: json["name"]). Factory constructors cannot use this to refer to the instance being created, so they rely on delegating to other constructors or returning existing instances.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Flutter codebase.