'static constructor in Dart

How to write static constructor in Dart?

class Generator
{
   static List<Type> typesList = [];

   //static
   //{ /*static initializations*/}

}


Solution 1:[1]

You can initialize static members by calling the class.member directly, inside the constructor:

class A {
  static int a;
  static int b;

  A(int a, int b) {
    A.a ??= a; ///by using the null-equals operator, you ensure this can only be set once
    A.b ??= b;
  }
}


main(){
  A(5,10);
  A(2,4);
  assert(A.a == 5);
  assert(A.b == 10);
}

Solution 2:[2]

Inspired by @Mahmoud salah eldien saber answer Create a singleton, and static variable reference to the singleton variable

void main() {
  
  print('${Singleton.add()}');
  print('${Singleton.add()}');
  print('${Singleton.add()}');
  print('${Singleton.add()}');     
}

class Singleton {
  Singleton();
    
  //static
  static List<int> typeList = Singleton.internal()._typeList;
  static List<int> add() {
    typeList.add(1);
    return typeList;
  }  
  List<int> _typeList = [];
  factory Singleton.internal() {  
    var s = Singleton();
    for(int i = 0 ; i < 5; i++ ) {
      s._typeList.add(2);
    }   
    return s;
  }    
   
}

I also want to find a official answer for this question.

Solution 3:[3]

Static variable declarations are initialized lazily to avoid costly initialization (and attendant slowness) at program startup.. The first time a static variable v is read, it is set to the result of evaluating its initializer.

https://groups.google.com/a/dartlang.org/forum/#!topic/misc/dKurFjODRXQ

Solution 4:[4]

One of the workarounds to initialize static variables could be to use ".." (called "cascade") operator. This workaround doesn't require object instance. More about cascade operator: https://dart.dev/guides/language/language-tour#cascade-notation .

Example:

static List firstList = [ 'hello', 12, 'goodbye'];

static List dummyObjects = List()
    ..addAll(firstList)
    ..add('another String value')
    ..add('and one more')
    ..add(Object())
    ..removeWhere((o) => o is! String)
;

(So dummyObjects is initialized only with objects that are of type String because all others discarded.)

Solution 5:[5]

To create static (constuctor or class) it's very easy in dart, just create static vaiable in this class and give it object from the same class:

class A {
  static var instance = A();
  static List<int> typesList = [];
  A() {
    print('Call constructor to create object');
  }
  List<int> add() {
    typesList.add(1);
    return typesList;
  }
}

void main() {
  print(A.instance.add());
  print(A.instance.add());
  print(A.instance.add());
  print(A.instance.add());
}

Solution 6:[6]

static initialization block

In Dart there is no equivalent to the Java static initialization block.
However, an easy workaround for most cases is extracting the static initialization logic to a static function.

class Dummy {
  static final Map<int, String> someValues = _createSomeValues();

  static Map<int, String> _createSomeValues() {
    Map<int, String> someValues = {};
    for (var i = 0; i <= 10; i++) {
      someValues[i] = "$i";
    }
    return someValues;
  }
}

Note that the static variable is initialized lazily as described in https://groups.google.com/a/dartlang.org/forum/#!topic/misc/dKurFjODRXQ. However, this behavior should never impact you with normal/clean code.

static method as factory

If you meant named constructors by 'static constructor' take a look at the following answer: https://stackoverflow.com/a/59811076/1218254

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 E_C
Solution 2 Ken Yeung 826
Solution 3 Arif
Solution 4
Solution 5 Mahmoud Salah Eldin
Solution 6 Thomas