Dart Programming

Dart IO

The library in Dart is a collection of routines or programming instructions. Dart has several built-in libraries that are useful for storing routines (functions, classes, and so on) and are often used. Constants, methods, properties, errors, and typesets are all found in a Dart library, as well as a collection of classes. We must first import the library into the current program before using it. The import keyword is provided by Dart and is used to get the library accessible in the current file. We are going to discuss one of the most commonly used dart libraries, which is the dart: io file library, from this article.

What is Dart: IO Library in Dart in Ubuntu 20.04?

Dart includes an ‘io’ standard library that comprises many classes and methods for reading and writing terminal input. This library includes support for File, HTTP, sockets, and other I/O for server applications. This library isn’t intended for use in web browsers. We shouldn’t need to import it directly because it’s already there.

The normal input stream reads the data from the keyboard synchronously as well as asynchronously. In the programming language Dart, the readLineSync() function takes user input via the console. This function’s definition is stored in the ‘dart: io’ library; you must import it into your code to access its functions and objects. If you don’t do this, the compiler will throw an error, and the program won’t run. The ‘dart: io’ library is utilized in the Flutter and standalone Dart VM programs.

How to Use the Dart: IO File in Dart in Ubuntu 20.04?

We can import the library into our program using the import command like this.

import dart: io

This is the general representation of including the dart: io file in the dart code script. This file must be included in the dart script header section. The following dart code script examples will help you understand the function of the dart: io library file in the dart programming language:

Example # 1: Standard Input in a Dart in Ubuntu 20.04

We can use the readLineSync() function in the Dart programming language to get the user’s standard input via the console. To get input from the console, we will need to import the dart: io library from the Dart libraries. While the stdin() class allows the user to synchronously and asynchronously read data from standard input. One of the ways to read user input is readLineSync().

As is obvious in the above program, our step is to import the standard input-output file ‘dart: io’ in the header section. Then, the main function is implemented. We have printed the statement that asks the user to enter their name. The stdin.readLineSync method is invoked here where the string “my_name” is declared. This stdin.readLineSync method gets the standard input entered by the user, and also, standard input can be read by the user. After that, the name will be printed.

The shell asks the user to enter the name as we have entered the name “saeed raza,” which can be read. After that, it displays the user name with the statement on the shell screen.

Example # 2: Input Integer From the User in a Dart in Ubuntu 20.04

The preceding code script has the standard input of a string. Here, we are taking an integer as standard input. Consider the following example of implementation.

The dart: io is required for standard input or output, so the code script has imported the dart: io library. This file allows us to access standard input in the dart program. There is the main function definition where the statement is printed which asks the user to enter the phone number. Then, we have an int type variable with the null safety operators “?” or “!” just to ensure the number entered by the user are numeric digits. The int.parse() function changes input values that cannot be null into integer values. The stdin.readLineSync method scans the integer input by the user and displays the integer input on the screen by the print statement.

The output shell shows the numbers entered by the user.

Example # 3: Standard Out in a Dart in Ubuntu 20.04

The standard output prints the number of lines and the line segments to the stdout and the output screen. Here, we are using the print() and stdout() methods. The print() function prints the specified string on the screen, making it ideal for the web. In contrast, the write(), writeln(), writeAll(), and addStream() functions all require the use of stdout. However, we don’t use stdout with the print function.

The code script has included the dart: io file for standard out. Then, inside the code script main parentheses, we used the print function and passed the statement of string under a double quote. After that, we used the second method, ‘stdout’; we used stdout with the write method. The write() method converts any data type into a string.

The shell screen shows the representation of two standard output methods as follows:

Example # 4: Printing the Addition in a Dart in Ubuntu 20.04

In this particular example, we are making a simple addition program for the dart. The program will use the standard input method and print the addition result through the standard output method.

The dart: io has been included in the program’s header. After that, the program’s main is invoked where we have two print functions are used to print the statements. After that stdin.readLineSync method is called to get the input from the user. The variable “add” is declared and uses the plus operator to add the two values from the user. The print function will print the result of the addition.

After taking input integers from the user, the sum of the integer has been displayed.

Conclusion

At last, we have ended our dart io article here. A detailed demonstration has been given with the introduction and definition. Then, we have described the representation of the dart io file in the dart program. The dart io file makes the standard input and output of the program accessible. We have four example illustrations where the standard output and input method are used while importing the dart io library file.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.