What is the right way variables initialize in stateless widget?

Viewed 577

I am initializes in constructor but I am not sure it is the right way?

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;
}
1 Answers

Yes, this is one way of initializing variables in Dart.

Dart as a language has many interesting features around constructors. Please see Using constructors for more info

flutter is a framework build with dart, so the constructor rules of dart apply to flutter as well and so a StatelessWidget class is like any other dart class.

Related