Table of contents
1.
Introduction
2.
Standard Functions
2.1.
Standard Input
2.2.
Standard Output
2.3.
I/O Error Handling
3.
FAQs
3.1.
What is Stdin in Dart?
3.2.
Is there a float data type in Dart?
3.3.
In Dart, how do you check if a string is empty or not?
4.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Input-Output in Dart

Author Akshit Pant
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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".

FAQs

What is Stdin in Dart?

Stdin is a class in Dart programming language. The standard input stream can be read synchronously or asynchronously using Stdin.

 

Is there a float data type in Dart?

A 64-bit (double precision) floating-point number is represented by the Double data type in Dart. For instance, consider the value "10.10", The keyword double represents Floating-point literals.

 

In Dart, how do you check if a string is empty or not?

The String’s Property isEmpty can be used to determine whether or not a string is empty. It returns True if the string is empty. If the string is not empty, False is returned.

Conclusion 

In this article, we learned about how Input-Output works in the Dart programming language.
You can head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build future applications.
We hope this article has helped you enhance your knowledge of Dart programming and Input-Output in it. If you want to learn more, check out our Programming Fundamentals and Competitive Programming articles. Do upvote this article to help other ninjas grow.

Happy Coding!

Live masterclass