Reputation: 61
I'm trying to get some currency data from a foreign exchange API. After getting data from the website, I'm having a problem in extracting the desired value of currency pair from the list which I'm receiving after the HTTP request as data. Here's what I am doing:
Future<void> getInfo() async {
try {
print((url)); //url = EUR/USD, this value is comeing from a dropdown
URL =
"https://fcsapi.com/api-v2/forex/latest?symbol=$url&access_key=t58zo1uMFJZlNJxSrSmZv2qIUlSkCk9RAfCLkwnMwt1q1FFS";
print((URL));
Response response =
await http.get(Uri.encodeFull(URL),
headers: {"Accept": "application/json"});
data = jsonDecode(response.body);
print(data);
List info = data['response'];
print(info);
double price = ......; // I don't know what to write here I have tried everything.
print(price);
here I want to get the value of the price from the list "info" but idont know what to write in 'double price'
} catch (e) {
print('error is : $e');
}
}
this is the result on consol
I/flutter ( 4286): EUR/USD
I/flutter ( 4286): https://fcsapi.com/api-v2/forex/latest?
symbol=EUR/USD&access_key=t58zo1uMFJZlNJxSrSmZv2qIUlSkCk9RAfCLkwnMwt1q1FFS
I/flutter ( 4286): {status: true, code: 200, msg: Successfully,
response: [{
id: 1,
price: 1.1788,
change: +0.0072,
chg_per: +0.61%,
last_changed: 2020-10-05 14:47:57,
symbol: EUR/USD}],
info: {server_time: 2020-10-05 14:49:12 UTC,
credit_count: 1, _t: 2020-10-05 14:49:12 UTC}}
// this is printing due to print (data); statemet
I/flutter ( 4286): [{
id: 1,
price: 1.1788, // i want to print this value
change: +0.0072,
chg_per: +0.61%,
last_changed: 2020-10-05 14:47:57,
symbol: EUR/USD}]
// this is printing due to print (info); statemet, which is printing the responce of json
what i want :
i want to print the value of price in console after printing the responce e.g.
I/flutter ( 4286): 1.1788
Upvotes: 0
Views: 1934
Reputation: 41
since the response is a list / array, you can use index to get items from the list.
var item = info[0]; // get first item from the list.
double price = item["price"];
print(price);
Upvotes: 0