Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

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