×

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 Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Dart Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes 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 Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

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

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart Type Test Operators

Type Test Operators in Dart These operators are used to check the types of expressions at runtime. Dart is a typed language, and we often want to assert that a value...

1 minute read.

Dart Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

4 minutes read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

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 Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

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.

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.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

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