What is factory constructot in DART

In this article, i am going to explain factory constructor in DART language.
  • 4025

Factory constructor in DART

The factory constructor does not create a new instance of the class bur returns an instance like singleton pattern. If you want to create a factory constructors, then use the factory keyword.

Example of factory constructor

void main() {

  factconst obj = new factconst();

  obj.val=25;

  print(obj.val);

}

 class factconst{

  static factconst fconstuct;

  var val;

  factory factconst(){

    if(fconstuct==null){

      fconstuct= new factconst._internal();

    }

    return fconstuct;

  }

  factconst._internal(){

   

  }
}


Output:
 

25

Ask Your Question 
 
Got a programming related question? You may want to post your question here
 
© 2019 DotNetHeaven. All rights reserved.