Flutter:尝试使用 jsonDecode - 错误:字符串不是索引错误类型 int 的子类型

问题描述

我想在 Flutter 中使用 CoinMarketCap API。但是我想将地图中的数据添加到列表中,会发生一个错误,它说: 'string' 类型不是 'index' 的 'int' 类型的子类型。 这是我的代码,我使用了本教程 Migrating to the new CoinMarketCap API with Flutter

 Future<void> getCryptoPrices() async{
 List cryptoDatas = [];
  print('Crypto Prices are Loading...');
String apiURL= "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";
http.Response response = await http.get(apiURL,headers: {'X-CMC_PRO_API_KEY': 'api code'});

  Map<String,dynamic> responseJSON = json.decode(response.body);
  if (responseJSON["status"]["error_code"] == 0) {
    for (int i = 1; i <= responseJSON["data"].length; i++) {
      cryptoDatas.add(responseJSON["data"][i.toString()]); // THE ERROR WILL HAPPEND HERE
}
 }
setState(() {
this.cryptoList = cryptoDatas; 
print(cryptoList);
});

提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)