How to write static constructor in Dart?
class Generator
{
static List<Type> typesList = [];
//static
//{ /*static initializations*/}
}
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.
If you meant named constructors by 'static constructor' take a look at the following answer: https://stackoverflow.com/a/59811076/1218254