【问题标题】:type 'int' is not a subtype of type 'double' (flutter)“int”类型不是“double”类型的子类型(颤振)
【发布时间】:2020-09-29 01:28:13
【问题描述】:

我正在使用 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;
  }
}

我认为我的问题来自美元类,所以我像这样更改了美元类:

................
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"

帮帮我!!!

【问题讨论】:

  • 问题是您试图在 int var 中获取的数据实际上是双精度类型,您可以分享 json 响应,以便我们可以准确地复制问题
  • 如果你没有访问REST API,你可以在你的属性中设置动态类型

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


【解决方案1】:

也许你在字符串类型字段中输入了双精度类型值。

double price = json['price'];

替换为

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

它会正常工作的。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      如果您的值 val 属于 dynamic 类型,它来自 Firestore 之类的来源,并且您确定它适合双精度,那么最简单的做法是:

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

      【讨论】:

        【解决方案4】:

        我想你应该试试num 数据类型,这样无论你从 API 获取整数还是双精度,这个数据类型都能正确处理。

        【讨论】:

          【解决方案5】:

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

          【讨论】:

          • 谢谢兄弟,我在 Data 类中把 double 改成了 var
          猜你喜欢
          • 2021-08-12
          • 2021-02-25
          • 2023-03-31
          • 2020-03-18
          • 1970-01-01
          • 1970-01-01
          • 2022-10-13
          • 2019-08-14
          • 2020-08-11
          相关资源
          最近更新 更多