2

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.

Elam
  • 413
  • 6
  • 19
  • Can you provide full state widget that will reproduce the same issue ? – Md. Yeasin Sheikh May 26 '22 at 12:16
  • Are you using the content on `showDialog`? you can include only relevant part that will reproduce the error. You can check more about [minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example) – Md. Yeasin Sheikh May 26 '22 at 12:55
  • Yes, i am using it in showGeneralDialog. – Elam May 26 '22 at 17:46
  • You can use `double.tryParse(amountController.text) ?? 0` to avoid error if the `TextField` is empty. – Miftakhul Arzak May 27 '22 at 02:32
  • Still i am getting the same error. Below i have added my code `double _availableValue = 12450.0; @override void initState() { super.initState(); amountController.text = _availableValue.toString(); amountController.addListener(_setAmountValue); } void _setAmountValue() { if ( double.parse(amountController.text).roundToDouble() >= minValue && double.parse(amountController.text).roundToDouble() <= maxValue ) { setState(() { _availableValue = double.tryParse(amountController.text) ?? 0; }); } }` – Elam May 27 '22 at 02:45

1 Answers1

1

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,))
                ],);
Golden Lion
  • 3,840
  • 2
  • 26
  • 35