I'm trying to accomplish what I though would be simple before I started learning flutter.
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
}
So while I can use the function getData to print the data that it contains, I can't print out the 'exchangeRate' value. Therefore I too can't insert the value into the Text widget.
Can anyone explain me in a simple words how is that working and how I can get the value of 'exchangeRate' variable and use it in the code?
Futurecoming fromgetData()method so you have to useFutureBuilderas in the link i posted