如何在 flutter 中从 apiServices 中提取确切的内容

how to extract exact contents from apiServices in flutter

我从 api

得到的回复
{
   "status":{
      "code":200
   },
   "errors":{
      "validation":null
   },
   "params":{
      "date":null,
      "from":"2021-07-20",
      "to":"2021-07-20",
      "post_type":null,
      "per_page":"10",
      "page":"1",
      "slug":null,
      "q":null
   },
   "data":{
      "payload":[
         {
            "date":"2021-07-20",
            "published_on":"2021-07-20 00:00:36",
            "modified_on":"2021-07-19 16:44:34",
            "rates":[
               {
                  "currency":{
                     "iso3":"INR",
                     "name":"Indian Rupee",
                     "unit":100
                  },
                  "buy":"160.00",
                  "sell":"160.15"
               },
               {
                  "currency":{
                     "iso3":"USD",
                     "name":"U.S. Dollar",
                     "unit":1
                  },
                  "buy":"119.50",
                  "sell":"120.10"
               },
               {
                  "currency":{
                     "iso3":"EUR",
                     "name":"European Euro",
                     "unit":1
                  },
                  "buy":"140.71",
                  "sell":"141.41"
               },
               {
                  "currency":{
                     "iso3":"GBP",
                     "name":"UK Pound Sterling",
                     "unit":1
                  },
                  "buy":"163.89",
                  "sell":"164.71"
               },
               {
                  "currency":{
                     "iso3":"CHF",
                     "name":"Swiss Franc",
                     "unit":1
                  },
                  "buy":"129.72",
                  "sell":"130.37"
               },
               {
                  "currency":{
                     "iso3":"AUD",
                     "name":"Australian Dollar",
                     "unit":1
                  },
                  "buy":"87.86",
                  "sell":"88.30"
               },
               {
                  "currency":{
                     "iso3":"CAD",
                     "name":"Canadian Dollar",
                     "unit":1
                  },
                  "buy":"93.37",
                  "sell":"93.84"
               },
               {
                  "currency":{
                     "iso3":"SGD",
                     "name":"Singapore Dollar",
                     "unit":1
                  },
                  "buy":"87.74",
                  "sell":"88.18"
               },
               {
                  "currency":{
                     "iso3":"JPY",
                     "name":"Japanese Yen",
                     "unit":10
                  },
                  "buy":"10.89",
                  "sell":"10.94"
               },
               {
                  "currency":{
                     "iso3":"CNY",
                     "name":"Chinese Yuan",
                     "unit":1
                  },
                  "buy":"18.42",
                  "sell":"18.51"
               },
               {
                  "currency":{
                     "iso3":"SAR",
                     "name":"Saudi Arabian Riyal",
                     "unit":1
                  },
                  "buy":"31.86",
                  "sell":"32.02"
               },
               {
                  "currency":{
                     "iso3":"QAR",
                     "name":"Qatari Riyal",
                     "unit":1
                  },
                  "buy":"32.25",
                  "sell":"32.41"
               },
               {
                  "currency":{
                     "iso3":"THB",
                     "name":"Thai Baht",
                     "unit":1
                  },
                  "buy":"3.64",
                  "sell":"3.66"
               },
               {
                  "currency":{
                     "iso3":"AED",
                     "name":"UAE Dirham",
                     "unit":1
                  },
                  "buy":"32.53",
                  "sell":"32.70"
               },
               {
                  "currency":{
                     "iso3":"MYR",
                     "name":"Malaysian Ringgit",
                     "unit":1
                  },
                  "buy":"28.29",
                  "sell":"28.43"
               },
               {
                  "currency":{
                     "iso3":"KRW",
                     "name":"South Korean Won",
                     "unit":100
                  },
                  "buy":"10.37",
                  "sell":"10.42"
               },
               {
                  "currency":{
                     "iso3":"SEK",
                     "name":"Swedish Kroner",
                     "unit":1
                  },
                  "buy":"13.71",
                  "sell":"13.78"
               },
               {
                  "currency":{
                     "iso3":"DKK",
                     "name":"Danish Kroner",
                     "unit":1
                  },
                  "buy":"18.92",
                  "sell":"19.01"
               },
               {
                  "currency":{
                     "iso3":"HKD",
                     "name":"Hong Kong Dollar",
                     "unit":1
                  },
                  "buy":"15.38",
                  "sell":"15.46"
               },
               {
                  "currency":{
                     "iso3":"KWD",
                     "name":"Kuwaity Dinar",
                     "unit":1
                  },
                  "buy":"397.41",
                  "sell":"399.40"
               },
               {
                  "currency":{
                     "iso3":"BHD",
                     "name":"Bahrain Dinar",
                     "unit":1
                  },
                  "buy":"316.98",
                  "sell":"318.57"
               }
            ]
         }
      ]
   },
   "pagination":{
      "page":1,
      "pages":1,
      "per_page":10,
      "total":1,
      "links":{
         "prev":null,
         "next":null
      }
   }
}

我正在尝试使用 response.body 从上方访问费率,如下所示。但我对访问费率感到困惑。任何帮助将不胜感激。

class ExchangeRateApiServices{
  static const String BASE_URL = "https://www.nrb.org.np/api/forex/v1";

  static Future  fetchExchangeRateList({String date}) async {
    String url = "/rates?page=1&per_page=10&from=$date&to=$date";
    print("Date: $date");
    List<Map<String, dynamic>> exchangeRateList = [];
    //List<Post> posts = [];
    //List<Post> posts = List<Post>.empty(growable: true);
    try {
      var response = await http.get(Uri.parse(BASE_URL + url));
      print(response.body);
      if (response.statusCode == 200) {
        List<Map<String, dynamic>> body =
        List<Map<String, dynamic>>.from(jsonDecode(response.body));
        body.forEach((element) {
          exchangeRateList.add(element);
        });



        print("exchangeRate list from ApiServices $exchangeRateList}");
       
        return exchangeRateList;
        
      } else {
        
        print("response error while fetching exchange Rates");
      }
    } catch (e) {
      print('we get error save ${e.toString()}');
    }
  }
}

我只需要从数据> 支付> 汇率中提取并显示汇率。我在解析响应时真的很困惑。但是我不知道如何做??

您的回复不是 List 类型,而是 Map<String, dynamic> 类型。要获得 rates,您可以执行以下操作。

// Get response data
final Map<String, dynamic> data = response.body['data'];

// Get payloads
final List<Map<String, dynamic>> payloads = data['payload'];

// Get rates of first item of payloads
final List<Map<String, dynamic>> rates = payloads.first['rates'];

您的回复不是列表

只是一个Map<String, dynamic>

你的 payload 给你 listMapspayload 的每个元素中,您都有一个速率列表,因为您的有效负载只有一个 child,您应该在速率

上使用 foreach

它应该是这样的:

Map<String, dynamic> body = jsonDecode(response.body);
body['data']['payload'].first['rates'].forEach((element) {
    exchangeRateList.add(element);
});