Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 颤振能';t将字符串转换为双精度_Json_Api_Flutter_Parsing - Fatal编程技术网

Json 颤振能';t将字符串转换为双精度

Json 颤振能';t将字符串转换为双精度,json,api,flutter,parsing,Json,Api,Flutter,Parsing,大家好,我有一个简单的函数,它将从我的服务器接收double,然后它应该将这个十进制数解析为double,并将其添加到total amount变量中 for (int i = 0; i < purchase.length; i++){ currentTotalPrice = currentTotalPrice + purchase[i]['total_price']; } 我不知道为什么在我的代码的其他部分它工作

大家好,我有一个简单的函数,它将从我的服务器接收double,然后它应该将这个十进制数解析为double,并将其添加到total amount变量中

            for (int i = 0; i < purchase.length; i++){
              currentTotalPrice = currentTotalPrice + purchase[i]['total_price'];
            }
我不知道为什么在我的代码的其他部分它工作正常

这就是我接收数据的方式

Future<List> _fetchPurchases() async {
  final prefs = await SharedPreferences.getInstance();
  String accessToken = prefs.getString("accessToken");

  final response = await http.get(
    '$webSiteUrl/api/admin/purchases/${article['id']}/$_billNumber',
    headers: <String, String>{
      'Content-Type': 'application/json;',
      'Authorization': 'Bearer $accessToken',
    },
  );
  if(response.statusCode == 200){
    List purchases = json.decode(response.body);
    return purchases;
  }else{
    return showErrorMessage(context);
  }
}
Future\u fetchPurchases()异步{
final prefs=wait SharedPreferences.getInstance();
String accessToken=prefs.getString(“accessToken”);
最终响应=等待http.get(
“$webSiteUrl/api/admin/purchases/${article['id']}/$\u billNumber”,
标题:{
“内容类型”:“应用程序/json;”,
“授权”:“持有人$accessToken”,
},
);
如果(response.statusCode==200){
List=json.decode(response.body);
退货;
}否则{
返回消息(上下文);
}
}

注意:我知道构造函数的解决方案是创建一个类,然后将数据工厂化到自定义dart对象,但在某些情况下,我不能这样做,除非我重构了很大一部分代码,所以如果有像
toDouble()这样的直接方法
method它将是最好的解决方案

事实上,
toDouble
String
类中不存在,但是如果必须使用它,您可以创建一个扩展并导入它,类似这样

extension NumberParsing on String {
  double toDouble() {
    return double.parse(this);
  }
}
检查一下这个要点

基本上是它的double.parse()方法,不管怎么说,我试着直接做,但它工作正常,我不知道为什么,但昨天它没有^_*
extension NumberParsing on String {
  double toDouble() {
    return double.parse(this);
  }
}