Issue
I am trying to fetch network data from the coinAPI to get the exchange rate for 1btc to usd.. but after fetching the api and awaiting it in price screen so the ui will give the exchange rate… I get failed host lookup
This is the coin_data.dart file… contains the api and networking
import 'dart:convert';
import 'package:http/http.dart' as http;
const List<String> currenciesList = [
'AUD',
'BRL',
'CAD',
'CNY',
'EUR',
'GBP',
'HKD',
'IDR',
'ILS',
'INR',
'JPY',
'MXN',
'NOK',
'NZD',
'PLN',
'RON',
'RUB',
'SEK',
'SGD',
'USD',
'ZAR'
];
const List<String> cryptoList = [
'BTC',
'ETH',
'LTC',
];
const coinAPIUrl = 'https://rest.coinapi.io/v1/exchangerate';
const apiKey = 'APIKEY';
class CoinData {
Future<dynamic> getCoinData() async {
// http.Response response = await http.get(Uri.parse('$coinAPIUrl/BTC/USD?apikey=$apiKey'));
http.Response response = await http.get(Uri.parse('$coinAPIUrl/BTC/USD?apikey=$apiKey'));
if (response.statusCode == 200) {
var decodedData = jsonDecode(response.body);
var lastPrice = decodedData['rate'];
return lastPrice;
} else {
print(response.statusCode);
throw 'Problem with the get request';
}
}
}
This is the price_screen.dart file.. the actual UI and display of the exchange rate
import 'dart:io' show Platform;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ticker_bitcoin/coin_data.dart';
class PriceScreen extends StatefulWidget {
const PriceScreen({super.key});
@override
State<PriceScreen> createState() => _PriceScreenState();
}
class _PriceScreenState extends State<PriceScreen> {
late String selectedCurrency = 'USD';
DropdownButton<String> androidDropdown() {
List<DropdownMenuItem<String>> dropdownItems = [];
for (int i = 0; i < currenciesList.length; i++) {
String currency = currenciesList[i];
var newItem = DropdownMenuItem(
value: currency,
child: Text(currency),
);
dropdownItems.add(newItem);
}
return DropdownButton(
value: selectedCurrency,
items: dropdownItems,
onChanged: (value) {
setState(() {
selectedCurrency = value!;
});
},
);
}
CupertinoPicker iOSPicker() {
List<Text> pickerItems = [];
for (String currency in currenciesList) {
Text(currency);
pickerItems.add(Text(currency));
}
return CupertinoPicker(
itemExtent: 32.0,
onSelectedItemChanged: (selectedIndex) {
print(selectedIndex);
},
children: pickerItems,
);
}
// Widget getPicker() {
// if (Platform.isIOS) {
// return iOSPicker();
// } else if (Platform.isAndroid) {
// return androidDropdown();
// }
// return getPicker();
// }
String bitcoinValueInUSD = '?';
void getData() async {
try {
double data = await CoinData().getCoinData();
setState(() {
bitcoinValueInUSD = data.toStringAsFixed(0);
});
} catch (e) {
print(e);
}
}
@override
void initState() {
super.initState();
getData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('🤑 Coin Ticker'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(18.0, 18.0, 18.0, 0),
child: Card(
color: Colors.lightBlueAccent,
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 15.0, horizontal: 28.0),
child: Text(
'1 BTC = $bitcoinValueInUSD USD',
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
),
),
),
Container(
height: 125.0,
alignment: Alignment.center,
padding: const EdgeInsets.only(bottom: 30.0),
color: Colors.lightBlue,
child: Platform.isIOS ? iOSPicker() : androidDropdown(),
),
],
),
);
}
}
Solution
It means your device/emulator got disconnected to the internet. If your host is connected to internet but emulator is disconnected then a cold reboot will do the trick. You’ll find that under Device Manager > vertical ellipsis (â‹®) > Cold Boot Now button.
Answered By – rohanmech
Answer Checked By – Mildred Charles (FlutterFixes Admin)