Flutter从API获取数据以构建方法

时间:2020-09-12 10:23:13

标签: api flutter dart

在尝试学习颤振之前,我正在尝试完成我想做的简单的事情。

const apiKey = 'xxxxxx';
const coinApiURL = 'https://rest.coinapi.io/v1/exchangerate';

class _CurrencyState extends State<CurrencyScreen> {

void getData() async {
http.Response response = await http.get('$coinApiURL/BTC/USD?apikey=$apiKey');

String data = response.body;
double exchangeRate = jsonDecode(data)['rate'];
print(exchangeRate);
}

@override
  Widget build(BuildContext context) {
    getData(); // prints out the value
    print(exchangeRate); // error "Undefined name 'exchangeRate'"
    (...)
    child: Text($exchangeRate) // value has to be inserted here
  }

因此,尽管我可以使用功能getData来打印包含的数据,但无法打印出'exchangeRate'值。因此,我也无法将值插入“文本”小部件中。

任何人都可以用一个简单的词来解释我的工作原理,以及如何获取'exchangeRate'变量的值并将其用于代码中吗?

1 个答案:

答案 0 :(得分:1)

我更改了您的代码,以便在structure(list(Gene = c("ID-1", "ID-1", "ID-1", "ID-1", "ID-1", "ID-1", "ID-1", "ID-1", "ID-1", "ID-1", "ID-1", "ID-1", "ID-1", "ID-1", "ID-1", "ID-4", "ID-4", "ID-4", "ID-4", "ID-4", "ID-4", "ID-4", "ID-4", "ID-4", "ID-4", "ID-4", "ID-4", "ID-4", "ID-4", "ID-4", "ID-4", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-5", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-6", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7", "ID-7" ), mRNA = c(-0.181385669, -0.059647494, 0.104476117, -0.052190978, -0.040484945, 0.194226742, -0.501601326, 0.102342605, -0.127143845, -0.008523742, -0.102946211, -0.042894028, 0.002922923, -0.134394347, -0.214204393, -0.138122686, 0.203242361, 0.097935502, 0.147068146, -0.089430917, 0.331565412, -0.034572422, -0.129896329, 0.324191, 0.470108479, -0.027268223, 0.232304713, 0.090348708, 0.070848402, 0.181540708, -0.502255367, -0.267631441, -0.368647839, -0.040910404, -0.003983171, -0.003983171, -0.003983171, -0.14980589, -0.119449612, -0.309154214, -0.487589361, 0.272803506, -0.421733575, -0.467108567, 0.024868338, -0.156025729, -0.044680175, -0.206716896, -0.272014193, -0.230499883, -0.238597397, -0.118130949, 0.349957464, 0.349957464, 0.349957464, 0.172048587, -0.186226994, 0.16113822, -0.293029136, -0.111636253, -0.044189887, 0.081555274, -0.048106079, -0.05853566, 0.010407814, -0.066981809, -0.09828484, -0.315190986, -0.005102456, 0.221556197, 0.206584568, 0.206584568, 0.206584568, 0.102649006, -0.011777384, -0.36963487, -0.054853074, -0.230240699, -0.210508323, -0.208889919, -0.050763372, 0.023073782, -0.095118984, -0.091076071, -0.330257395), Time = structure(c(2L, 2L, 2L, 3L, 3L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 2L, 2L, 2L, 3L, 3L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 3L, 2L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 3L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), .Label = c("0", "20", "40", "60", "120"), class = "factor"), predicted_mRNA = c(-0.00551000342030954, -0.00551000342030954, -0.00551000342030954, -0.0302695238715682, -0.0302695238715682, -0.00551000342030954, -0.0302695238715682, -0.0302695238715682, -0.0550290443228268, -0.0550290443228268, -0.0550290443228268, -0.0550290443228268, -0.129307605676603, -0.129307605676603, -0.129307605676603, -0.00551000342030954, -0.00551000342030954, -0.00551000342030954, -0.0302695238715682, -0.0302695238715682, -0.00551000342030954, -0.0302695238715682, -0.0302695238715682, -0.0550290443228268, -0.0550290443228268, -0.0550290443228268, -0.0550290443228268, -0.129307605676603, -0.129307605676603, -0.129307605676603, -0.129307605676603, -0.00551000342030954, -0.00551000342030954, -0.00551000342030954, 0.0192495170309491, 0.0192495170309491, 0.0192495170309491, -0.0302695238715682, -0.0302695238715682, -0.00551000342030954, -0.0302695238715682, -0.0302695238715682, -0.0550290443228268, -0.0550290443228268, -0.0550290443228268, -0.129307605676603, -0.129307605676603, -0.129307605676603, -0.129307605676603, -0.00551000342030954, -0.00551000342030954, -0.00551000342030954, 0.0192495170309491, 0.0192495170309491, 0.0192495170309491, -0.0302695238715682, -0.0302695238715682, -0.00551000342030954, -0.0302695238715682, -0.0302695238715682, -0.0550290443228268, -0.0550290443228268, -0.0550290443228268, -0.0550290443228268, -0.129307605676603, -0.129307605676603, -0.129307605676603, -0.00551000342030954, -0.00551000342030954, -0.00551000342030954, 0.0192495170309491, 0.0192495170309491, 0.0192495170309491, -0.0302695238715682, -0.00551000342030954, -0.0302695238715682, -0.0302695238715682, -0.0550290443228268, -0.0550290443228268, -0.0550290443228268, -0.0550290443228268, -0.129307605676603, -0.129307605676603, -0.129307605676603, -0.129307605676603)), row.names = c(NA, -85L ), class = "data.frame") 完成处理http响应数据后构建Text小部件。

getData