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?
Just add toString() to your int.
The print() function is used to print the string on the screen. The string can be formatted message, any expression, and any other object. Dart provides ${expression}, which is used to put the value inside a string.
A string can be cast to an integer using the int. parse() method in Dart. The method takes a string as an argument and converts it into an integer.
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With