Response I got from 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
}
}
}
I am trying to access rates from the above-using response.body as below. But I am confused about accessing rates. Any help will be appreciated.
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()}');
}
}
}
All I need is to extract and display exchange rates from data> payment>rates. I am really confused about parsing the response. But I am not being able to figure out how to.
your response is not a list
it's just a Map<String, dynamic>
and your payload
give you a list
of Maps
and in each element of payload
, you have a list of rates and because your payload only have one child you should use foreach
on rates
it should look like this :
Map<String, dynamic> body = jsonDecode(response.body);
body['data']['payload'].first['rates'].forEach((element) {
exchangeRateList.add(element);
});