Introduction
Dart is a client-oriented programming language that may be used to develop apps for various platforms. It is object-oriented and may be compiled into JavaScript or native code.
In this blog, we will be covering how to take standard Input and Output in Dart. Before learning how we do so, learning the basic Introduction of Dart is recommended.
So, let us start reading about how Input-Output functions in Dart.
Standard Functions
Dart offers us a standard library called 'io' that has many classes and methods for reading. Using the import command, we can import the library into our program.
Syntax:
Import 'dart:io';
Standard Input
The .readLineSync() function in the Dart programming language allows you to take input from the user via the console. To get input from the console, you will need to import the Dart:io library as shown above.
Stdin Class: This Stdin class allows the user to synchronously and asynchronously read data from standard input. One of the methods used to take the input from the user is readLineSync(). Consider this example:
Code:
import 'dart:io'; // importing std. lib.
void main() // main method
{
String my_name = stdin.readLineSync(); // using readLineSync() to take input
print("Hey There, $my_name \n Welcome To Code Studio"); // printing line
}
Input:
Ninja
Output:
Hey There, Ninja
Welcome To Code Studio
This is how we can use the console to take a string(“Ninja" here) as input.
We will now see how to take an integer value as the input in the console in the example below:
Code:
import 'dart:io'; // importing std. lib.
void main()
{
int my_number = int.parse(stdin.readLineSync()); // readLineSync() method
print("The Entered number is $my_number"); // print line
}
Input:
142
Output:
The Entered number is 142
Standard Output
There are two possible ways to show output in the console in Dart.
- By using the print statement,
- By using stdout.write() method.
Let us see this with an example:
Code:
import 'dart:io'; // importing std. lib.
void main() // main method
{
// 1. Print using print statement
print("1. Code Studio Presents");
// 2. Print using stdout.write()
stdout.write("2. Blog on Dart Input/Output");
}
Output:
1. Code Studio Presents
2. Blog on Dart Input/Output
Here we can see how we can use both the print statement and stdout.write() method to print in the console. However, note down the fact that the main difference between the print statement and stdout.write() method is that the print() statement brings down the cursor to the next line(just like println() in java) while stdout.write() do not bring the cursor to the next line(just like simple print() of java); it remains in the same line.
I/O Error Handling
There could be a possibility that the user did not enter anything in the console for the standard input; for such situations, we need to check if the user entered nothing, and we can do so using the null aware operators provided by the Dart. Let us see this with an example given below:
Code:
import 'dart:io'; // importing std. lib.
void main() // main method
{
stdout.write("Hey there, What is your name?\n");
var p_name;
p_name = p_name ?? stdin.readLineSync();
p_name.isEmpty ? stdout.write('Enter your name\n') : stdout.write('Thankyou ${p_name}');
// Here ? is for null safety
}
Output:
Hey there, What is your name?
Enter your name
In the above example, If we go into a situation where the user has not entered anything, we can send a regular message to a terminal that says, "Enter your name".