Arun George
Arun George

Reputation: 1219

Dart : Printing integer along with string

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?

Upvotes: 20

Views: 29276

Answers (4)

januarvs
januarvs

Reputation: 408

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

print('$key|${favoriteBooksBox.get(key)}');

Upvotes: 0

The EasyLearn Academy
The EasyLearn Academy

Reputation: 927

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); 

}

Upvotes: 0

Pawel Laskowski
Pawel Laskowski

Reputation: 6316

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");
}

Upvotes: 41

edmond
edmond

Reputation: 1592

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

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

Upvotes: 11

Related Questions