我想为此在类中初始化最终变量。
Class C{
final int variable1 = 0; //dynamically generated value
final int variable2;
C(): this.variable2 = this.variable1 + 1; //variable2 need access to "this" to initialize
}
最佳答案
您可以将私有(private)构造函数与公共(public)工厂构造函数一起使用。
class C {
final int variable1;
final int variable2;
C._(this.variable1, this.variable2);
factory C() {
var v1 = Random().nextInt(10);
return C._(v1, v1 + 1);
}
@override
String toString() => 'Instance of C $variable1 $variable2';
}
关于flutter - 将其初始化为dart中的最终变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61581359/