Home > OS >  Uncaught Error: TypeError: 100: type 'JSInt' is not a subtype of type 'String'
Uncaught Error: TypeError: 100: type 'JSInt' is not a subtype of type 'String'

Time:04-13

void main() {
 String num = "50";
  int quantity = 2;
  num = (int.parse(num) * quantity) as String;
  print("num is " num);
}

This is giving me error

Actually i want to convert a string to numerical value and perform calculations and update the string back with the updated value.

// Assume the intial value of 
// item.quantity = 1, item.cost = 20

-----------------------------------------------------------------

item.quantity  ;
item.cost = (int.parse(item.cost) * item.quantity) as String;

-----------------------------------------------------------------

====================================================
expected result item.cost = 40 
generated result is 2020
====================================================

CodePudding user response:

By using toString you create a new String value. By casting as T you tell the compiler that an unknown value you want as T value.

void main() {
 String numer = "50";
  int quantity = 2;
  numer = (num.parse(numer) * quantity).toString();
  print("sum is $numer"); // sum is 100
}

Note: avoid use names which is reserved by system to avoid conflict.

  • Related