Flutter 分割字符串时double无效
我在分割字符串FormatException:Invalid double时出错Flutter 分割字符串时double无效,flutter,dart,Flutter,Dart,我在分割字符串FormatException:Invalid double时出错 double planAvg = (double.parse(planlist[pos].total_amt).toString() / double.parse(planlist[pos].total_weight).toString()).toStringAsFixed(2); 这就是我如何实现的 planAvg == "" ? SizedBox() : Padding(
double planAvg = (double.parse(planlist[pos].total_amt).toString() / double.parse(planlist[pos].total_weight).toString()).toStringAsFixed(2);
这就是我如何实现的
planAvg == ""
? SizedBox()
: Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
"Avg : " +
planAvg,
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.normal,
),
),
),
如果有人能帮忙的话您正在将变换保持为双倍、前后交替 假设这是一个字符串:
planlist[pos]。总金额
String planAvg = (
double.parse(planlist[pos].total_amt) / double.parse(planlist[pos].total_weight)
).toStringAsFixed(2);
所以,您要做的是将它们都转换为double,进行除法运算,然后返回到String
相反,如果您想要双倍回报,只需:
double planAvg = double.parse(planlist[pos].total_amt) /
double.parse(planlist[pos].total_weight);
以下是出现错误的原因:
toString()
方法将双精度值转换为字符串值。/
运算符不能与字符串一起使用
toStringAsFixed(2)
,因此它会返回一个字符串值。因此,不能将其存储在双变量中
String planAvg=(double.parse(planlist[pos].total_amt)/double.parse(planlist[pos].total_weight)).toStringAsFixed(2);
是的,我希望将它们放入string中,但我仍然有一个错误,即“格式异常无效双精度”,对于第一段代码,您不应该有这个错误。可能planlist[pos]的内容。总金额和总权重不能转换为双精度,请尝试将它们的值可视化以调试错误。现在我编辑了我的代码,您可以检查一下它吗?还有planlist[pos]。总金额和planlist[pos]。总权重是动态的,两者都是字符串planAvg=double.parse(planlist[pos].total\u amt.isEmpty?“0”:计划列表[pos]。总金额)/double.parse(计划列表[pos]。总权重。isEmpty?“0”:计划列表[pos]。总权重)代码>啊,好的。您还想检查该值是否为空。