“ int”类型不是“ double”类型的子类型(颤振)

时间:2020-06-09 18:54:34

标签: json flutter dart dart-pub flutter-test

我正在使用CoinMarketCap API从服务器获取硬币的数据,所以我有问题! Android Studio会给我以下消息:

b.Baz

我的问题几乎来自我的模型,所以这是我的模型: 硬币课

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

然后android studio给我其他问题

................
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();
..............

这是测试的链接:

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

请帮帮我!

5 个答案:

答案 0 :(得分:5)

尝试打印服务器发送的JSON,并检查所有参数是否都具有期望的格式。例如如果特定参数的值是int,而您期望它的值为double,则将其更改为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返回的参数,并尝试将其初始化为字符串变量。

答案 1 :(得分:1)

我想您应该尝试使用num数据类型,以便无论您是从API获取整数还是双精度数据,该数据类型都将正确处理它。

答案 2 :(得分:1)

存储在 json 中的数据属于 Number 类型。您可以检查数据是否存在,然后使用以下代码将它们转换为双精度:

((json['price'] as num) ?? 0.0).toDouble(),

答案 3 :(得分:0)

如果您有一个val类型的值dynamic,它来自Firestore之类的资源,并且您肯定可以将其加倍,那么最简单的方法是:

double result = double.parse('${val}');

答案 4 :(得分:0)

也许您在 String 类型字段中放置了 double 类型值。

double price = json['price'];

替换为

json['price'] != null ? double.parse(json['price']) : 0.0

它会正常工作。