Flutter:比较运算符

问题描述 投票:0回答:1

我正在尝试使用比较运算符验证表单,但它给了我这个错误。

The operator '>' isn't defined for the type 'String'.
Try defining the operator '>'


String fare = ''; //the value I want
double amount = totalDistance * 0.25; //code for total distance has already been taken care of
    String amountPerPass = amount.toStringAsFixed(0);
    setState(() {
      fare = amountPerPass;
    });

效果很好,我得到了

fare
。 但是,我想使用
fare
来验证表单。但它不起作用。有帮助吗?

TextFormField(
validator: (value) 
{if (value!.isEmpty) 
{return ('Enter the amount per passenger');}
if (value > fare) //The error occurs here with the operator sign. How can I correct it?
{   
return ('Your price should not exceed $fare');
}
},
)
flutter validation operators
1个回答
0
投票

value

中的
TextFormField.validator
String?
类型。你应该首先检查它的可空性,然后你可以做
double.tryParse
int.tryParse
,这取决于你是否接受浮点数。

如果字符串无效,这将返回其中一种数字类型或 null。

但是请注意,如果您决定接受浮点值,不同的语言会使用不同的格式并使用不同的字符作为千位分隔符和小数点分隔符。

© www.soinside.com 2019 - 2024. All rights reserved.