如何在 Flutter 中使用 api 中的双精度值?

How to use a double value from an api in Flutter?

我觉得问这个有点傻。因为我能够从 api 中解析地图和列表,但我不知道如何从相同的 api.

中使用像 double 这样简单的东西

我只是想做一个

double yld;

从这个api link,

https://sandbox.iexapis.com/stable/data-points/market/DGS10?token=Tsk_38ddda0b877a4510b42a37ae713cdc96

响应正文是 String,因此您必须使用 double.parse()

将其解析为 double
getYld() async {
  var url = 'https://sandbox.iexapis.com/stable/data-points/market/DGS10? 
  token=Tsk_38ddda0b877a4510b42a37ae713cdc96';
  var res = await http.get(url);
  if (res.statusCode == 200) {
     double yld = double.parse(res.body);  // parse the double here
  }
}