type 'int' is not a subtype of type 'double' (flutter)

11,122

Solution 1

Try to print the JSON which is sent by the server and check if all the parameters are having the expected format. For e.g. if a particular parameter has int value and you are expecting it as double then change it to double. You can change the code of initialization of double parameter as following

data['price'] = this.price;

to

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

This will fix the error type 'int' is not a subtype of type 'double'
To fix type 'double' is not a subtype of type 'String', you need to find out the parameter which is returned as a double and you are trying to initialize it to a string variable.

Solution 2

I guess you should try num data type so whether you get integer or double from the API, this data type will handle it properly.

Solution 3

If you have a value val of the dynamic type which is coming from a source like Firestore, which you're sure fits into a double, then the easiest thing to do is :

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

Solution 4

Data that are stored in json are of type Number. You can check if data exist and then cast them to double with this code:

((json['price'] as num) ?? 0.0).toDouble(),
Share:
11,122
ABDULKARIM ALBAIK
Author by

ABDULKARIM ALBAIK

Software engineer Android/Flutter Developer Flutter Package Publisher Backend Developer (Nodejs)

Updated on June 05, 2022

Comments

  • ABDULKARIM ALBAIK
    ABDULKARIM ALBAIK almost 2 years

    I am using CoinMarketCap API to fetch coin's data from server ,so i have a problem ! Android studio give me this message:

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

    My problem almost from my model ,so this my models: Coin class

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

    Data class

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

    Platform class

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

    Quote class

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

    Status class

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

    USD class

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

    I think my problem from USD class ,so i changed USD class like this :

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

    Then android studio give me other problem

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

    This is link to test :

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

    Give me help guys !!!