Flutter convert int variable to string

2020-03-13 08:26发布

问题:

I'm new with Flutter and I wonder about how difficult is (for me) to find a solution on how to convert my variable var int counter = 0; into a variable var String $counter = "0";

I searched a lot but more at all I only found something like var myInt = int.parse('12345');

that doesn't work with var myInt = int.parse(counter);

回答1:

Use toString and/or toRadixString

  int intValue = 1;
  String stringValue = intValue.toString();
  String hexValue = intValue.toRadixString(16);

or, as in the commment

  String anotherValue = 'the value is $intValue';


回答2:

// String to int

String s = "45";
int i = int.parse(s);

// int to String

int j = 45;
String t = "$j";

// If the latter one looks weird, look into string interpolation on https://dart.dev



回答3:

You can use the .toString() function in the int class.

int age = 23;
String tempAge = age.toString();

then you can simply covert integers to the Strings.



标签: dart flutter