×

Dart Standard Input & Output

Standard Input in Dart (stdin):

The standard input stream reads data both synchronously and asynchronously from the keyboard. 

In Dart programming language, .readLineSync( ) function is used to accept input from the user via console. The definition of this function is stored in the ‘dart:io’ library; therefore, to use its functions and objects, you need to import this library in your code. If you fail to do so, the compiler will report an error, and the program will not execute. 

The ‘dart:io’ library is used in the code that runs in Flutter and the standalone Dart VM. 

readLineSync( ) Function

This function reads a line (a string) from stdin and blocks it until the user passes the text and presses return. It returns a non-nullable string value. 

But what about the integer values? 

For integer values, we use int.parse( ) function along with the readLineSync( ) function as its argument. 

int.parse( ) function takes string values that cannot be null and converts them into integer values. 

1. For Strings = The string values are accepted from the user using stdin class with .readLineSync( ) function. 

Syntax :  

String? Variable = stdin.readLineSync( );

Consider the following Dart code that inputs string from the user: 

import 'dart:io';

void main( )

{

  print( ' Enter your favourite coding language : ' );

  // inputs string from the user

  string? code_lang = stdin.readLineSync( );

  // Printing the string

  print( '\nGreat! $code_lang is your favourite language!' );

}

Output:

Great! Dart is your favourite language!

2. For Integer values = The integer values are accepted from the user using int.parse( ) function with .readLineSync( ) function. 

Syntax : 

int? variable = int.parse( readLineSync( ) ! );

Observe that, in this syntax we have used ‘!’ after readLineSync( ) function. This is because, int.parse( ) function cannot accept strings that can be null as per Null Safety feature. 

Consider the following code that inputs integer value from the user: 

import 'dart:io';

void main( )

 {

 print( 'Enter the value of a : ' );

 int? a = int.parse(stdin.readLineSync( )! ); // accepts integer value

 print( 'Enter the value of b : ' );

 int? b = int.parse( stdin.readLineSync( )! ); // accepts integer value

 int mul = a * b; // stores product of a and b in mul

 print( 'Product of these two numbers is : $mul' );

}

Output :

Enter the value of a :

15

Enter the value of b :

12

Product of these two numbers is : 180

Standard output in Dart (stdout):

The standard output writes the number of lines to stdout and the text lines to the output screen. 

There are various functions provides by stdout: 

  1. write( )
  2. writeln( )
  3. writeAll( )
  4. addStream( )
  5. print( )

The write( ) and writeln( ) functions take any type of data and print their equivalent string value. 

The writeAll( ) function prints a given list of objects, while the addStream( ) function prints the elements of a particular stream asynchronously. 

The print( ) function, most suitable for the web, prints the given string on the screen. 

Important point to note here is that print( ) and writeln( ) functions can print the strings in next line. While write( ) function prints them in the same line. 

It is mandatory to use stdout with write( ), writeln( ), writeAll( ) and addStream( ) function. While, with print we do not use stdout. 

Example : 

Consider the following code: 

import 'dart:io';

void main( ) {

 // write( ) function

 stderr.write( ' This illustrates ' ) ;

 stdout.write( ' write( ) function. \n ' ) ;

 // writeln( ) function

 stdout.writeln( ' This illustrates ' ) ;

 stdout.writeln( ' writeln( ) function. \n ' ) ;

 // print( ) function

 print( ' This illustrates ' ) ;

 print( ' print( ) function. ' ) ;

}

Output

This illustrates write() function.

This illustrates

writeln( ) function.

This illustrates

print( ) function.

Standard error in Dart (stderr)

The standard error prints the error messages to the console. It has the same functions as stdout and works in a similar manner. 

Consider the following code: 

import 'dart:io';

void main( ) {

 // write( ) function

 stderr.write( 'This is ' );

 stderr.write( 'an error message \n' );

 // writeln( ) function

 stderr.writeln( ' This is ' ) ;

 stderr.writeln( 'an error message \n' );

}

Output

This is an error message

This is an error message

Related Topics

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

4 minutes read.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Dart Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

3 minutes read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes read.

Dart Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 minutes read.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Rust vs Dart

Rust is a system-level programming language that stands next to C ++ in terms of syntax, but offers greater speed and memory security. Dart, on the other hand, is an...

2 minutes read.

Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Dart Future and Stream

Before understanding Future and Stream in Dart, we need first to get familiar with two terms : Synchronous and Asynchronous Programming.  In synchronous programming, a single operation is handled at a...

9 minutes read.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.