dart 在无状态小部件中初始化变量的正确方法是什么?

e0bqpujr  于 2023-03-15  发布在  其他
关注(0)|答案(1)|浏览(124)

我在构造函数中初始化,但我不确定这是正确的方式?

class PlaceDialogWidget extends StatelessWidget {
  final NearbyPlace currentPlace;
  final SizeHelper sizeHelper;

  PlaceDialogWidget(
      {Key? key, required this.currentPlace, required this.sizeHelper})
      : dialogWidth = sizeHelper.width! * 0.7,
        dialogHeight = sizeHelper.height! * 0.5,
        super(key: key);

  late final double dialogWidth;
  late final double dialogHeight;
}
2guxujil

2guxujil1#

是的,这是在Dart中初始化变量的一种方法。
作为一种语言,Dart在构造函数方面有很多有趣的特性。请参阅使用构造函数了解更多信息
flutter是使用dart构建的框架,因此dart的构造函数规则也适用于flutter,因此StatelessWidget类与任何其他dart类类似。

相关问题