如何从飞镖/颤振列表中获取价值?

时间:2020-10-05 15:16:49

标签: android flutter http dart flutter-layout

我正在尝试从外汇API获取一些货币数据。从网站获取数据后, 我在从HTTP请求后收到的列表中提取货币对的期望值时遇到问题,将其作为数据。这是我在做什么:

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);

在这里,我想从列表“信息”中获取价格的值,但是idont知道在“双倍价格”中写什么

} catch (e) {
  print('error is : $e');
}
}

这是在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

我想要什么:

我想在打印响应后在控制台中打印价格值,例如

     I/flutter ( 4286): 1.1788

2 个答案:

答案 0 :(得分:0)

这将起作用

import static org.assertj.core.api.Assertions.assertThat;
import static us.abstracta.jmeter.javadsl.JmeterDsl.*;

import java.time.Duration;
import org.eclipse.jetty.http.MimeTypes.Type;
import org.junit.jupiter.api.Test;
import us.abstracta.jmeter.javadsl.TestPlanStats;

public class PerformanceTest {

  @Test
  public void testPerformance() throws IOException {
    TestPlanStats stats = testPlan(
      threadGroup(2, 10,
        httpSampler("http://my.service")
          .post("{\"name\": \"test\"}", Type.APPLICATION_JSON)
      ),
      //this is just to log details of each request stats
      jtlWriter("test.jtl")
    ).run();
    assertThat(stats.overall().elapsedTimePercentile99()).isLessThan(Duration.ofSeconds(5));
  }
  
}

答案 1 :(得分:0)

由于响应是列表/数组,因此可以使用索引从列表中获取项目。

var item = info[0]; // get first item from the list.
double price = item["price"];
print(price);