How to write static constructor in Dart?
class Generator
{
static List<Type> typesList = [];
//static
//{ /*static initializations*/}
}
How to write static constructor in Dart?
class Generator
{
static List<Type> typesList = [];
//static
//{ /*static initializations*/}
}
There is no such thing as a static constructor in Dart. Named constructors such as Shape.circle()
are achieved by something like
class A {
A() {
print('default constructor');
}
A.named() {
print('named constructor');
}
}
void main() {
A();
A.named();
}
You might also be interested in this factory constructors question
Update: A couple of static initializer work-arounds
class A {
static const List<Type> typesList = [];
A() {
if (typesList.isEmpty) {
// initialization...
}
}
}
Or the static stuff can be moved out of the class if it isn't meant to be accessed by users of the class.
const List<Type> _typesList = [];
void _initTypes() {}
class A {
A() {
if (_typesList.isEmpty) _initTypes();
}
}
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);
}
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.
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
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.)
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://mcmap.net/q/697604/-static-constructor-in-dart
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());
}
You could use a static method like this:
class A {
A() {
print('normal constructor');
}
static A fromValue(String value){
... //use your value
return A();
}
}
void main() {
final a = A.fromValue('myValue');
}
© 2022 - 2024 — McMap. All rights reserved.