User Input, Null Safety & Arithmetic Operators in Dart Explained
Understanding the Implementation of User Input, using Null Safety, and Arithmetic Operators in Dart Programming Language in 2025
In this tutorial, I have explained how to take input from the user in Dart. It is important to understand that user input is always taken as String which is then converted into the desired data type.
We use stdin class from IO library of dart in order to take input from user through keyboard, and readLineSync() method to take input as string. Let’s see its implementation in the code below.
Firstly, show a meaningful message to user to stimulate him/her to give input.
print(“What is your name”);
After that, take the user input of desired data type.
stdin.readLineSync();
Above line allows user to enter the name. Note that you need to store the user input in some variable as well, so that you can access it. In this case, you should create a variable userName since you have to take name of the user as input.
String userName = stdin.readLineSync();
Above line of code will render compile time error because the stdin.readLineSync() method returns the nullable string since the Dart supports Null Safety. Therefore, you have to convert the user input in the non-nullable String.
String userName = stdin.readLineSync().toString();
toString() method converts everything in string that is put after. Let’s now understand what is Null Safety in Dart.
Null Safety
Dart introduced Null Safety in the release of its 2.12. It basically avoids the programming from encountering unintentional errors. Let’s understand it example code.
String? userName;
The question mark at the end of String data type is a sign of nullable. It shows that userName variable may contain null value.
String userName = “User Name”;
Above variable is not nullable because it has initial value, and it can’t be null. In nullable values, when you access them, you have to handle the null case. Let’s see following example.
String? userName; // I have declared a nullable string
I have printed the nullable value in following code where userName variable hasn’t been assigned a value therefore, it is null. You can’t print null but you can print null as string by using toString() method.
print(userName.toString()); // expected output is null (as string)
Other way to handle nullable variables is through conditions. You can keep the check through if else condition where you can only print the nullable variable if that is not null.
if (userName != null){
print(userName);
}
else
{
print(““);
}
Above program prints the value of userName variable it is not null. In other case it will print blank. Let now continue our main topic that is to print user input and perform arithmetic operations on them.
So, I have mentioned above how you can take user input and assign that value to a string variable. Let’s now see how can you do that for other data types such int, double, and bool.
Convert User Input in Integer
As I have mentioned above that the value you get from user is always in string. You can’t assign a string value to int or double. For that you need to convert that value to the desired data type.
print(“Enter your age“);
int age = int.parse(stdin.readLineSync().toString());
After writing a prompt for user to enter the age, I have used parse method of int class, which converts the value inside it into the int. In this case it has converted the value of age entered by user into int data type. You can do the same with other data types.
For Double
print(“Enter your pecentage“);
double percentage = double.parse(stdin.readLineSync().toString());
For Boolean
print(“Result Status: Pass or Fail?“); // expect true or false from user
bool age = bool.parse(stdin.readLineSync().toString());
Note that if you enter string value while entering the percentage then you may encounter compile time error because dart will not be able to convert string type into double. I mean if the “Name”, this can’t be converted into double since it is not double in Mathematics. Similarly, if you enter “Name” when entering Boolean value where you should enter true or false then also you will encounter an error since dart can’t convert “Name” into bool. You must understand dart variables in order understand this concept.
Now that you how to take input from the user. Let’s know understand what are arithmetic operators, how they are used in programming.
Arithmetic Operators in Dart:
Arithmetic Operators are predefined symbols in dart which are used to perform specific functions such as addition, subtraction, multiplication, division, and mod. Addition is used to add the numbers. Its sign is “+”. Subtraction is used to subtract the numbers. Its sign is “-“. Multiplication is used to multiply different numbers, and its sign is “*”. Division is used to divide the numbers and its sign is “/”. Finally, “%” is used to find remainder. The sign is called mod. You can see code implemenation below.
print(“Enter number x: “);
double x = double.parse(stdin.readLineSync().toString());
print(“Enter number y: “);
double y = double.parse(stdin.readLineSync().toString());
final double sum = x + y;
final double sub = x — y;
final double mul = x * y;
final double division = x / y;
final double mod = x % y;
print(“value of X is $x”);
print(“value of Y is $y”);
print(“$x + $y = $sum”);
print(“$x — $y = $sub”);
print(“$x * $y = $mul”);
print(“$x / $y = $division”);
print(“$x % $y = $mod”);
Conclusion:
So far, I have described how to take input from the user, nature of input, how to convert that input to different data types, and store that input in a variable. In addition, I have also briefly explained null safety, and arithmetic operators. I hope you understand the concepts, and are able to implement them in your programming course. Thank you for reading this article. Stay Blessed.