gpt4 book ai didi

json - 'int' 类型不是 'double' 类型的子类型( flutter )

转载 作者:行者123 更新时间:2023-12-03 04:10:59 25 4
gpt4 key购买 nike

我正在使用 CoinMarketCap API 从服务器获取硬币数据,所以我遇到了问题!
Android studio 给我这个消息:

type 'int' is not a subtype of type 'double'

我的问题几乎来自我的模型,所以这是我的模型:
硬币类
import 'Data.dart';
import 'Status.dart';

class Coin {
Status status;
List<Data> data;

Coin(this.status, this.data);

factory Coin.fromJson(Map<String, dynamic> json) {

var data = new List<Data>();
if (json['data'] != null) {
json['data'].forEach((v) {
data.add(new Data.fromJson(v));
});
}

return Coin(
json['status'] != null ? new Status.fromJson(json['status']) : null,
data
);

}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.status != null) {
data['status'] = this.status.toJson();
}
if (this.data != null) {
data['data'] = this.data.map((v) => v.toJson()).toList();
}
return data;
}
}

数据类
import 'Platform.dart';
import 'Quote.dart';

class Data {
int id;
String name;
String symbol;
String slug;
int numMarketPairs;
String dateAdded;
List<String> tags;
double maxSupply;
double circulatingSupply;
double totalSupply;
Platform platform;
int cmcRank;
String lastUpdated;
Quote quote;

Data(
this.id,
this.name,
this.symbol,
this.slug,
this.numMarketPairs,
this.dateAdded,
this.tags,
this.maxSupply,
this.circulatingSupply,
this.totalSupply,
this.platform,
this.cmcRank,
this.lastUpdated,
this.quote);

factory Data.fromJson(Map<String, dynamic> json) {

int id = json['id'];
String name = json['name'];
String symbol = json['symbol'];
String slug = json['slug'];
int numMarketPairs = json['num_market_pairs'];
String dateAdded = json['date_added'];
List<String> tags = json['tags'].cast<String>();
double maxSupply = json['max_supply'];
double circulatingSupply = json['circulating_supply'];
double totalSupply = json['total_supply'];
Platform platform = json['platform'] != null
? new Platform.fromJson(json['platform'])
: null;
int cmcRank = json['cmc_rank'];
String lastUpdated = json['last_updated'];
Quote quote = json['quote'] != null ? new Quote.fromJson(json['quote']) : null;

return Data(
id,
name,
symbol,
slug,
numMarketPairs,
dateAdded,
tags,
maxSupply,
circulatingSupply,
totalSupply,
platform,
cmcRank,
lastUpdated,
quote
);

}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
data['symbol'] = this.symbol;
data['slug'] = this.slug;
data['num_market_pairs'] = this.numMarketPairs;
data['date_added'] = this.dateAdded;
data['tags'] = this.tags;
data['max_supply'] = this.maxSupply;
data['circulating_supply'] = this.circulatingSupply;
data['total_supply'] = this.totalSupply;
if (this.platform != null) {
data['platform'] = this.platform.toJson();
}
data['cmc_rank'] = this.cmcRank;
data['last_updated'] = this.lastUpdated;
if (this.quote != null) {
data['quote'] = this.quote.toJson();
}
return data;
}
}

平台类
class Platform {
int id;
String name;
String symbol;
String slug;
String tokenAddress;

Platform(this.id, this.name, this.symbol, this.slug, this.tokenAddress);

factory Platform.fromJson(Map<String, dynamic> json) {

int id = json['id'];
String name = json['name'];
String symbol = json['symbol'];
String slug = json['slug'];
String tokenAddress = json['token_address'];

return Platform(
id,
name,
symbol,
slug,
tokenAddress
);


}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
data['symbol'] = this.symbol;
data['slug'] = this.slug;
data['token_address'] = this.tokenAddress;
return data;
}
}

报价类
import 'USD.dart';

class Quote {
USD uSD;

Quote(this.uSD);

factory Quote.fromJson(Map<String, dynamic> json) {

return Quote(
json['USD'] != null ? new USD.fromJson(json['USD']) : null
);

}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.uSD != null) {
data['USD'] = this.uSD.toJson();
}
return data;
}
}

状态类
class Status {
String timestamp;
int errorCode;
Null errorMessage;
int elapsed;
int creditCount;
Null notice;

Status(
this.timestamp,
this.errorCode,
this.errorMessage,
this.elapsed,
this.creditCount,
this.notice);

factory Status.fromJson(Map<String, dynamic> json) {

String timestamp = json['timestamp'];
int errorCode = json['error_code'];
Null errorMessage = json['error_message'];
int elapsed = json['elapsed'];
int creditCount = json['credit_count'];
Null notice = json['notice'];

return Status(
timestamp,
errorCode,
errorMessage,
elapsed,
creditCount,
notice
);
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['timestamp'] = this.timestamp;
data['error_code'] = this.errorCode;
data['error_message'] = this.errorMessage;
data['elapsed'] = this.elapsed;
data['credit_count'] = this.creditCount;
data['notice'] = this.notice;
return data;
}
}

美元类
class USD {
double price;
double volume24h;
double percentChange1h;
double percentChange24h;
double percentChange7d;
double marketCap;
String lastUpdated;

USD(
this.price,
this.volume24h,
this.percentChange1h,
this.percentChange24h,
this.percentChange7d,
this.marketCap,
this.lastUpdated);

factory USD.fromJson(Map<String, dynamic> json) {

double price = json['price'];
double volume_24h = json['volume_24h'];
double percent_change_1h = json['percent_change_1h'];
double percent_change_24h = json['percent_change_24h'];
double percent_change_7d = json['percent_change_7d'];
double market_cap = json['market_cap'];
String last_updated = json['last_updated'].toString();

return USD(
price,
volume_24h,
percent_change_1h,
percent_change_24h,
percent_change_7d,
market_cap,
last_updated
);

}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['price'] = this.price;
data['volume_24h'] = this.volume24h;
data['percent_change_1h'] = this.percentChange1h;
data['percent_change_24h'] = this.percentChange24h;
data['percent_change_7d'] = this.percentChange7d;
data['market_cap'] = this.marketCap;
data['last_updated'] = this.lastUpdated;
return data;
}
}

我认为我的问题来自美元类(class),所以我改变了美元类(class):
................
double price = double.parse(json['price']);
double volume_24h = double.parse(json['volume_24h']);
double percent_change_1h = double.parse(json['percent_change_1h']);
double percent_change_24h = double.parse(json['percent_change_24h']);
double percent_change_7d = double.parse(json['percent_change_7d']);
double market_cap = double.parse(json['market_cap']);
String last_updated = json['last_updated'].toString();
..............

然后android studio给我其他问题
type 'double' is not a subtype of type 'String' 

这是测试链接:
https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=..........&start="+index.toString()+"&limit=10"

给我帮助伙计们!!!

最佳答案

尝试打印服务器发送的 JSON 并检查所有参数是否具有预期格式。例如如果特定参数具有 int value 并且您期望它为 double然后将其更改为两倍。您可以更改 double 的初始化代码参数如下

data['price'] = this.price;



json['price'] == null ? 0.0 : json['price'].toDouble() // forcefully convert int to double

这将修复错误 type 'int' is not a subtype of type 'double'修复 type 'double' is not a subtype of type 'String' ,您需要找出作为 double 返回的参数并且您正在尝试将其初始化为字符串变量。

关于json - 'int' 类型不是 'double' 类型的子类型( flutter ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62289899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com