首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在dart/flutter中从列表中获取值?

如何在dart/flutter中从列表中获取值?
EN

Stack Overflow用户
提问于 2020-10-05 23:16:50
回答 2查看 285关注 0票数 0

我正在尝试从外汇API中获取一些货币数据。从网站获取数据后,我在从列表中提取货币对的期望值时遇到了问题,该列表是在HTTP请求之后作为数据接收的。下面是我正在做的事情:

代码语言:javascript
复制
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);

这里我想从列表"info“中获取价格的值,但我不知道在”双倍价格“中写什么。

代码语言:javascript
复制
} catch (e) {
  print('error is : $e');
}
}

这是控制台上的结果

代码语言:javascript
复制
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

我想要的:

我想在打印回复后在控制台中打印价格的值。

代码语言:javascript
复制
     I/flutter ( 4286): 1.1788
EN

回答 2

Stack Overflow用户

发布于 2020-10-05 23:21:31

这将会起作用

代码语言:javascript
复制
double price = info[0]['price']
票数 0
EN

Stack Overflow用户

发布于 2020-10-05 23:24:41

因为响应是一个列表/数组,所以您可以使用index从列表中获取项。

代码语言:javascript
复制
var item = info[0]; // get first item from the list.
double price = item["price"];
print(price);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64211462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档