将字符串格式的负数转换为双精度



当我使用double时。解析我的文本字段输入,我得到以下错误FormatException (FormatException:无效双-)。看到一个类似的帖子,但问题似乎没有解决

如何防止用户输入两个"。"one_answers";产生绯闻;. 非常感谢任何帮助。占用您的时间了

TextFormField(
inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'^-?d*.?d*')),],
keyboardType: TextInputType.numberWithOptions(signed: true,decimal: true),
onChanged:(value) {
setState(() {
data = double.parse(value);
});
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'data'
),
),

我尝试使用RegExp('[0-9.-]'),但它不起作用。我仍然得到与上面相同的错误信息

Parse需要一个double类型的字符串。因此,当您输入点(.)或破折号(-)并试图将其解析为双精度数时,它会抛出异常。您需要应用一些检查来解决这个问题。

试试这个

TextFormField(
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^-?d*.?d*')),
],
keyboardType:
TextInputType.numberWithOptions(signed: true, decimal: true),
onChanged: (value) {
if (value.isNotEmpty && value != '.' && value != '-') {
setState(() {
data = double.parse(value);
});
}
},
decoration:
InputDecoration(border: OutlineInputBorder(), labelText: 'data'),
),

最新更新