使用 http.dart 调用 CoinMarketCap API。不知道该怎么办

Using http.dart to Call CoinMarketCap API. Not sure what to do

所以我是 flutter 和 dart 的新手,我正在尝试从 CoinMarketCap API 调用。我正在使用 HTTP 包来调用数据和 API。我对它们不是很熟悉,但这是我想出的...

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

我遇到了一些错误:

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'

我是不是没有成功导入JSON?我不确定如何修复错误。我需要做什么才能成功拨打 API 电话?任何反馈都会很棒。

  1. 'Payload'不是扑class所以不存在。您是否尝试使用定制 Class?

  2. 'payloadFromJson' 错误意味着它不存在,所以如果它在另一个 class

    中,您可能没有正确导入它
  3. Undefined name 'data' 意思是数据还没有定义如果你想要响应的正文使用'response.body'

代码已更新 #1

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(int limit) async {
    var url = '$_url/listings/latest?start=1&limit=$limit&convert=USD';
    var result = await HttpReqService.get<JMap>(
      url, 
      auth: AuthType.apiKey,
      authData: MapEntry('X-CMC_PRO_API_KEY', _apiKey)
    );
    var coins = (result?['data'] as List).cast<JMap>().map<String>((e) => e['name']);
    print(coins);
  }
}

void main(List<String> args) async {
  await CoinMarketCap.getListingLatest(7);
  print('\nJob done!');
}

输出:

(Bitcoin, Ethereum, Tether, USD Coin, BNB, XRP, Cardano)

Job done!