Flutter将int变量转换为字符串

att*_*ila 6 dart flutter

我是Flutter的新手,我想知道(对我来说)如何解决如何将变量var int counter = 0转换为解决方案有多困难?放入变量var String $ counter =“ 0”;

我进行了很多搜索,但根本没有发现类似var myInt = int.parse('12345');的内容。

不适用于var myInt = int.parse(counter);

特别感谢您的建议

Ric*_*eap 13

使用toString和/或toRadixString

  int intValue = 1;
  String stringValue = intValue.toString();
  String hexValue = intValue.toRadixString(16);
Run Code Online (Sandbox Code Playgroud)

或者,如在纪念中

  String anotherValue = 'the value is $intValue';
Run Code Online (Sandbox Code Playgroud)

  • 你也可以使用字符串插值来转换它,它更简洁一点:`String stringValue = '$intValue';` (2认同)

bra*_*b0y 13

// 字符串转int

String s = "45";
int i = int.parse(s);
Run Code Online (Sandbox Code Playgroud)

// 整数转字符串

int j = 45;
String t = "$j";
Run Code Online (Sandbox Code Playgroud)

// 如果后者看起来很奇怪,请查看https://dart.dev 上的字符串插值


Ach*_*uru 6

您可以在 int 类中使用 .toString() 函数。

int age = 23;
String tempAge = age.toString();
Run Code Online (Sandbox Code Playgroud)

那么你可以简单地将整数转换为字符串。