Home > Net >  Initializing a variable to a named parameter
Initializing a variable to a named parameter

Time:11-20

I am needing to initialize double val to equal sliderValue, passed in above.

class SliderContainer extends StatefulWidget {
  const SliderContainer({Key? key, required this.sliderValue}) : super(key: key);
  
   final String sliderValue;

  @override
  _SliderContainerState createState() => _SliderContainerState();
}



class _SliderContainerState extends State<SliderContainer> {
  double val = double.parse(widget.sliderValue); //need to initialize to this value
  var round = 0;
  String dim = 'dimension string';

  @override

  Widget build(BuildContext context) {


Of course, I can't use widget.sliderValue outside of the build. How do I do this?

CodePudding user response:

initialize inside initState:

@override
void initState() {
   super.initState();
   val = double.parse(widget.sliderValue);
}
  • Related