26
votes

Flutter convertit la variable int en chaîne

Je suis nouveau avec Flutter et je me demande à quel point il est difficile (pour moi) de trouver une solution sur la façon de convertir ma variable var int counter = 0; dans une variable var String $ counter = "0";

J'ai beaucoup cherché mais plus du tout je n'ai trouvé que quelque chose comme var myInt = int.parse ('12345');

cela ne fonctionne pas avec var myInt = int.parse (counter);


0 commentaires

4 Réponses :


42
votes

Utilisez toString et / ou toRadixString

  String anotherValue = 'the value is $intValue';

ou, comme dans le commentaire

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


1 commentaires

Vous pouvez également utiliser l'interpolation String pour le convertir, c'est un peu plus concis: String stringValue = '$intValue';



5
votes

// Chaîne en int
int j = 45;
String t = "$j";

// int en chaîne

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

// Si ce dernier a l' air bizarre , regardez l'interpolation de chaîne sur https://dart.dev


0 commentaires

4
votes

Vous pouvez utiliser la fonction .toString () dans la classe int.

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

alors vous pouvez simplement convertir des entiers en chaînes.


0 commentaires

0
votes
int i=10;
String st = i.toString(); // Converted to String

0 commentaires