Factory and named constructor for json mapping in dart

1,971

Solution 1

A Dart class may have generative constructors or factory constructors. A generative constructor is a function that always returns a new instance of the class. Because of this, it does not utilize the return keyword.

A factory constructor has looser constraints than a generative constructor. The factory need only return an instance that is the same type as the class or that implements its methods (ie satisfies its interface). This could be a new instance of the class, but could also be an existing instance of the class or a new/existing instance of a subclass (which will necessarily have the same methods as the parent). A factory can use control flow to determine what object to return, and must utilize the return keyword. In order for a factory to return a new class instance, it must first call a generative constructor.

Please see also Understanding Factory constructor code example - Dart for a very detailed explanation.

So for your question: Yes it is returning a new Instance but i guess the speciality comes from the fact that you the factory constructor is capable of creating an object based on an incoming json map whereas the generative constructor is used to instantiate a new object from single attributes.

And for your last question: Both do the same, namely returning an instance of the class given a json map. The techincal difference is that one is a generative and one a factory constructor.

Solution 2

One of the use cases when to use factory constructor or named constructor. To initialise the final fields of the class you have to do it in initializer list or in the declaration when using named constructor. On the other hand using factory constructor You can initialise the final fields in the body of the constructor.

Share:
1,971
Frozen Forest
Author by

Frozen Forest

Updated on December 25, 2022

Comments

  • Frozen Forest
    Frozen Forest over 1 year

    Consider this code

    class Album {
      int userId;
      int id;
      String title;
    
      Album({this.userId, this.id, this.title});
    
      Album.fromJsonN(Map<String, dynamic> json) {
        this.userId = json['userId'];
        this.id = json['id'];
        this.title = json['title'];
      }
      factory Album.fromJson(Map<String, dynamic> json) {
        return Album(userId: json['userId'], id: json['id'], title: json['title']);
      }
    }
    

    In most tutorials the explanation for why we use factory for json mapping method is: "we use the factory keyword when implementing a constructor that doesn’t always create a new instance of its class".

    in factory method in above code, doesn't it returning a new instance? if it does, so whats the reason for using factory here? and whats the difference between factory constructor and fromJsonN named constructor in this context?

  • Frozen Forest
    Frozen Forest over 3 years
    i saw stackoverflow.com/a/56107639/7755519 and other related posts before. the reasons for using factory generally are return subclass instance, return existing instance if available and create singleton! but in factory fromJson constructor if i understood correctly we have not reasons to use it! does it have any benefit such as performance or memory management or etc? or we use factory just for more clarity?
  • Julian2611
    Julian2611 over 3 years
    So semantically they are both the same, yes. The main point for using the factory constructors is, if you want to implement the factory-pattern (see also [here])(en.wikipedia.org/wiki/Factory_method_pattern). In general I suggest there is no further technical advantage in using factory per se. But since factor constructors are generally used to control the instance creation. You may cache instances and return already existing instances for observing such memory and performance effects. For the very details please also refer to dash-overflow.net/articles/factory