如何在颤动中将TextFormField的数据作为双精度获取

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

它 returns 没有。

textformfield 文本值为:1,133.00

它还说“尝试拨打:<(1133.0) 1,133.00”

在文本字段中使用键盘类型作为数字可能对您有所帮助

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

您需要从字符串中删除“,”,然后在解析为双精度后。

double _pointsUsing = double.parse(pointsEdditingController.text.replaceAll(",",""));

你可以这样做:

print( double.tryParse('2,456.90'.replaceAll(",","")));