78

I'm trying to convert an integer variable

var int counter = 0;

into a string variable

var String $counter = "0";

I searched but I only found something like

var myInt = int.parse('12345');

that doesn't work with

var myInt = int.parse(counter);

3 Answers 3

117

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';
1
  • 2
    You can also use String interpolation to convert it, it's a little more concise: String stringValue = '$intValue';
    – greyaurora
    Mar 2, 2019 at 0:58
26

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

0
13

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.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.