Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 !!!

like image 667
ABDULKARIM ALBAIK Avatar asked Jun 09 '20 18:06

ABDULKARIM ALBAIK


People also ask

Is Int a subtype of Double?

android - type 'int' is not a subtype of type 'double' - Stack Overflow.

How do you convert an int to a double in flutter?

To convert an int to a double , use toDouble() method. The result is an integer with one decimal point ( . 0 ). The result is a double with one decimal point ( .

How do you convert a string to a double in flutter?

For converting a String to a double, the parse() static method of double class can be used. String source, [@deprecated double onError(String source)?] );

Is int a type of double in flutter?

type 'int' is not a subtype of type 'double' (flutter) Ask Question Asked1 year, 6 months ago Active7 months ago Viewed10k times 6 I am using CoinMarketCap API to fetch coin's data from server ,so i have a problem !

Is int a subtype of double?

type ‘int’ is not a subtype of type ‘double’. To fix this error you could just add .0 to every number you have in your code. For example if you have 500 turn it into 500.0.

How to fix'int'is not a subtype of type'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. it to a string variable. type 'double' is not a subtype of type 'int?'

Is future<int> a subtype of type 'int'?

type 'Future<int>' is not a subtype of type 'int?' flutter type 'double' is not a subtype of type 'int?' Unhandled Exception: type 'double' is not a subtype of type 'Double?'


1 Answers

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.

like image 172
Sanjay Sharma Avatar answered Nov 15 '22 05:11

Sanjay Sharma