简体   繁体   English

当我更改值时,Flutter 会抛出“Invalid double”错误

[英]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.提前致谢。

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,))
                ],);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM