📱 Flutter
Intermediate
What is a factory constructor in Dart?
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.