So I'm new to flutter and dart and am trying to call from the CoinMarketCap API. I'm using the HTTP package to call the data and the API. I'm not super familiar with them but here's what I came up with...
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<Payload> getCryptoPrices() async {
var response = await http.get(Uri.parse(
"https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=5000&convert=USD"),
headers: {
'X-CMC_PRO_API_KEY': 'my-key',
"Accept": "application/json",
});
if (response.statusCode == 200) {
Payload payload = payloadFromJson(data.body);
return payload;
}
}
I get a couple of errors:
The name 'Payload' isn't a type so it can't be used as a type argument
The function 'payloadFromJson' isn't defined
Undefined name 'data'
Am I not successfully importing JSON? I'm not sure how to fix the error. What do I need to do to successfully make a API Call? Any feedback would be great.
CodePudding user response:
'Payload' is not a flutter class so it does not exist. were you trying to use a Custom made Class?
the 'payloadFromJson' error means it does not exist so you probably did not import it properly if it is in another class
Undefined name 'data' means that data has not been defined if you want the body of the response use 'response.body'
CodePudding user response:
import 'package:wnetworking/wnetworking.dart';
class CoinMarketCap {
static const _apiKey = '111111111111111111111111111111';
static const _url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency';
static Future<void> getListingLatest() async {
var url = '$_url/listings/latest?start=1&limit=1&convert=USD';
var result = await HttpReqService.get<JMap>(
url,
auth: AuthType.apiKey,
authData: MapEntry('X-CMC_PRO_API_KEY', _apiKey)
);
print(result);
}
}
void main(List<String> args) async {
await CoinMarketCap.getListingLatest();
print('\nJob done!');
}
Output:
{status: {timestamp: 2022-05-17T03:09:39.597Z, error_code: 0, error_message: null, elapsed: 20, credit_count: 1, notice: null, total_count: 10093}, data: [{id: 1, name: Bitcoin, symbol: BTC, slug: bitcoin, num_market_pairs: 9432, date_added: 2013-04-28T00:00:00.000Z, tags: [mineable, pow, sha-256, store-of-value, state-channel, coinbase-ventures-portfolio, three-arrows-capital-portfolio, polychain-capital-portfolio, binance-labs-portfolio, blockchain-capital-portfolio, boostvc-portfolio, cms-holdings-portfolio, dcg-portfolio, dragonfly-capital-portfolio, electric-capital-portfolio, fabric-ventures-portfolio, framework-ventures-portfolio, galaxy-digital-portfolio, huobi-capital-portfolio, alameda-research-portfolio, a16z-portfolio, 1confirmation-portfolio, winklevoss-capital-portfolio, usv-portfolio, placeholder-ventures-portfolio, pantera-capital-portfolio, multicoin-capital-portfolio, paradigm-portfolio], max_supply: 21000000, circulating_supply: 19041962, total_supply: 19041962, platform: null, cmc_rank: 1, self_reported_circulating_supply: null, self_reported_market_cap: null, last_updated: 2022-05-17T03:09:00.000Z, quote: {USD: {price: 30058.37671968433, volume_24h: 32078967435.36901, volume_change_24h: 14.6307, percent_change_1h: 0.2943742, percent_change_24h: -1.21229756, percent_change_7d: -3.39227709, percent_change_30d: -25.24067315, percent_change_60d: -25.89745416, percent_change_90d: -31.85557711, market_cap: 572370467277.9137, market_cap_dominance: 44.3959, fully_diluted_market_cap: 631225911113.37, last_updated: 2022-05-17T03:09:00.000Z}}}]}
Job done!