Search code examples
fluttertextfieldtexteditingcontroller

Flutter throws an "Invalid double" error when i change the value


I am trying to get the value from the Textfield and use that value in the slider. I have achieved what i want but i am getting error when i change the value of the textfield.

Error:

The following FormatException was thrown while dispatching notifications for TextEditingController:
Invalid double

Code:

  double minValue = 0.0;
  double maxValue = 25000.0;
  double _availableValue = 12450.0;

  void _setAmountValue() {
    print('Amount: ${double.parse(amountController.text)}');
    if (
        double.parse(amountController.text).roundToDouble() >= minValue &&
        double.parse(amountController.text).roundToDouble() <= maxValue
        ) {
      setState(() {
        _availableValue = double.parse(amountController.text).roundToDouble();
      });
    }
  }

TextField(
controller: amountController,
focusNode: amountFocus,
style: TextStyle(color: Color(0xff757575), fontSize: 15, fontFamily: 'Gilroy Medium'),
decoration: InputDecoration(hintText: "12,450.00",border: InputBorder.none,),maxLines: 1,),


Slider(
     value: availableValue.toDouble(),
     min: 0.0,
     max: 25000.0,
     onChanged: (double newValue) {
        setState(() {
          availableValue = newValue.toInt();
          // amountController.text = value.toString();
        });
      },
),

Please help me on this issue. Thanks in advance.


Solution

  • use the text controller to set the value from the slider

    void initState()
    {
      super.initState();
      _sliderTextController.text=_sliderValue.toString();
    }
    double _sliderValue=50.0;
    
    return Row(children: [
                      Slider(
                        min:0,
                        max:100,
                        value:_sliderValue,
                        onChanged: (value) => {
                         
                          setState((){_sliderValue=value;_sliderTextController.text=value.toString();})
                      },),
                      SizedBox(width:100,child:
                      TextFormField(controller: _sliderTextController,))
                    ],);