19

Consider the below code.

void main() {
  int num = 5;
  print('The number is ' + num);
}

When I try to print the value for the variable num, I get the exception : The argument type 'int' can't be assigned to the parameter type 'String'.

How do I go about in printing num?

Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
Arun George
  • 1,167
  • 3
  • 15
  • 29

4 Answers4

40

In order to print the value of the int along with the String you need to use string interpolation:

void main() {
  int num = 5;
  print("The number is $num");
}
Pawel Laskowski
  • 6,078
  • 1
  • 21
  • 35
10

Just add toString() to your int. Similar to JS.

void main() {
  int num = 5;
  print('The number is ' + num.toString()); // The number is 5
}
edmond
  • 1,432
  • 2
  • 15
  • 19
  • 1
    This is not allowed, throughs a error in VSCode. Use method given by Pawel L. – SSG Dec 28 '20 at 22:05
0
void main() {
int age = 10;
double location = 21.424567;
bool gender = true;
String name = "The EasyLearn Academy";

print(age); 
print(location);
print(gender); 
print(name); 

print("age $age"); 
print("location $location");
print("gender $gender"); 
print("name $name}"); 

print("age " + age.toString()); 
print("location " + location.toString());
print("gender " + gender.toString()); 
print("name " + name); 

}

0

Could use ${} in order to concatenate with return of function:

print('$key|${favoriteBooksBox.get(key)}');
januarvs
  • 398
  • 3
  • 7