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'

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

这给我错误

实际上我想将字符串转换为数值并执行计算并使用更新后的值更新字符串。

// 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
====================================================

通过使用 toString 您可以创建一个新的 String 值。通过转换 as T 你告诉编译器你想要一个未知值作为 T 值。

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.