Dart:打印整数和字符串

Dart : Printing integer along with string

考虑以下代码。

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

当我尝试打印变量 num 的值时,出现异常:The argument type 'int' can't be assigned to the parameter type 'String'.

我该如何打印数字?

只需将 toString() 添加到您的整数。类似于JS。

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

为了打印 int 的值以及您需要使用字符串插值的字符串:

void main() {
  int num = 5;
  print("The number is $num");
}
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); 

}