[英]How to get the data of TextFormField as a double in flutter
我尝试使用 double.parse 将 TextFormField 的数据作为双精度:
double _pointsUsing = double.parse(pointsEdditingController.text);
但它会导致错误说“方法'> ='在null上被调用”。 如果我尝试了这个:
double _pointsUsing = double.tryParse(pointsEdditingController.text);
它返回 null。
textformfield 文本值为:1,133.00
它还显示“尝试调用:<(1133.0) 1,133.00”
您需要从字符串中删除“,”,然后在解析后加倍。
double _pointsUsing = double.parse(pointsEdditingController.text.replaceAll(",",""));
你可以这样做:
print( double.tryParse('2,456.90'.replaceAll(",","")));
在文本字段中使用键盘类型作为数字,它可能会对您有所帮助
TextField(
decoration: new InputDecoration(
labelText: "Enter your number"),
keyboardType: TextInputType.number,
);
```
Then parse your data like this:-
String textFieldData = pointsEdditingController.text.isEmpty ? "0" : pointsEdditingController.text;
double _pointsUsing = double.parse(textFieldData);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.