【问题标题】:Flutter - using an API keyFlutter - 使用 API 密钥
【发布时间】:2019-02-18 10:18:52
【问题描述】:

我正在制作一个从 public v1 Api 获取加密货币 JSON 数据的应用程序,但很快就会放弃对此的支持,这意味着我将不得不迁移更强大的 professional v1 Api。

唯一的问题是,我不知道如何实现解析 JSON 数据时所需的新 Api 密钥的使用。

我正在使用此 git repo 的大量修改版本来编写应用程序,但所有基本功能都基于此处。

我所需要的只是关于我需要添加到此文件中以显示新的专业 v1 Api 的指导,任何 cmets 或想法都将受到赞赏。谢谢

这是 crypto_data_prod.dart 文件,我必须在其中更改我的代码以与密钥一起使用。

import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:fluttercrypto/data/crypto_data.dart';

class ProdCryptoRepository implements CryptoRepository {
  String cryptoUrl = "https://api.coinmarketcap.com/v1/ticker/?limit=50";
  @override
  Future<List<Crypto>> fetchCurrencies() async {
    // TODO: implement fetchCurrencies
    http.Response response = await http.get(cryptoUrl);
    final List responseBody = JSON.decode(response.body);
    final statusCode = response.statusCode;
    if (statusCode != 200 || responseBody == null) {
      throw new FetchDataException(
          "An error ocurred : [Status Code : $statusCode]");
    }

    return responseBody.map((c) => new Crypto.fromMap(c)).toList();
  }
}

【问题讨论】:

    标签: json dart flutter restful-authentication restful-url


    【解决方案1】:

    尝试将http.Response response = await http.get(cryptoUrl);改为

    http.Response response = await http.get(cryptoUrl, headers: {"X-CMC_PRO_API_KEY": "cab79c7b-52e9-4e4b-94fc-b0f32da14799"});

    更多信息请查看this link。

    【讨论】:

    • 我确定这可行,但事实证明 JSON 类型的样式与公共 V1 api 的样式不同,我现在收到错误 type '_InternalLinkedHashMap&lt;String, dynamic&gt;' is not a subtype of type 'List&lt;dynamic&gt;'
    • 嗨 Daniil,你运气好吗?
    • 如果有人需要听到答案:该调用用于返回一个数组(在 dart 中也称为列表),而现在它返回一个 JSON 对象(在 dart 中又称为 Map)。
    猜你喜欢
    • 1970-01-01
    • 2011-09-28
    • 2016-03-23
    • 2019-10-18
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多