3

I'm making an app that grabs cryptocurrency JSON data from the public v1 Api but support for this will soon be dropped, meaning that I'll have to migrate the the more powerful professional v1 Api.

The only issue is, I don't know how to implement the use of the new Api key thats required as I parse the JSON data.

I'm using a heavily modified version of this git repo to program the app, but all basic functionality is based here.

All I need is guidance on what I need to add to this file to display the new professional v1 Api, any comments or ideas are appreciated. Thanks

This the the crypto_data_prod.dart file where I would have to change my code for use with the key.

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();
  }
}
Khal
  • 49
  • 1
  • 1
  • 6
Jake
  • 1,906
  • 9
  • 32
  • 60

1 Answers1

3

Try to change http.Response response = await http.get(cryptoUrl); to

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

For more info check this link.

Danylo
  • 5,152
  • 2
  • 17
  • 13
  • I'm sure this works, but it turns out that the JSON type is styled differently to the public V1 api, I'm now getting the error `type '_InternalLinkedHashMap' is not a subtype of type 'List'` – Jake Sep 13 '18 at 17:18
  • Hi Daniil, any luck with this? – Jake Sep 14 '18 at 13:09
  • In case anyone needs to hear the answer: The call used to return an array (aka list in dart) while it now returns a JSON Object (aka Map in dart). – harlekintiger Nov 02 '21 at 12:09