Dart – Standard Input Output

Standard Input in Dart:

In Dart programming language, you can take standard input from the user through the console via the use of .readLineSync() function. To take input from the console you need to import a library, named dart:io from libraries of Dart.

About Stdin Class:
This class allows the user to read data from standard input in both synchronous and asynchronous ways. The method readLineSync() is one of the method use to take input from user. Refer to offical doc for other method, from here.

Taking a string input from user –

filter_none

edit
close

play_arrow

link
brightness_4
code

// importing dart:io file
import 'dart:io';
  
void main()
{
    print("Enter your name?");
    // Reading name of the Geek
    String name = stdin.readLineSync();
  
    // Printing the name
    print("Hello, $name! \nWelcome to GeeksforGeeks!!");
}

chevron_right


Input:

Geek

Output:



Enter your name?
Hello, Geek! 
Welcome to GeeksforGeeks!!

Taking integer value as input –

filter_none

edit
close

play_arrow

link
brightness_4
code

// Importing dart:io file
import 'dart:io';
  
void main()
{
    // Asking for favourite number
    print("Enter your favourite number:");
  
    // Scanning number
    int n = int.parse(stdin.readLineSync());
  
    // Printing that number
    print("Your favourite number is $n");
}

chevron_right


Input:

01

Output:

Enter your favourite number:
Your favourite number is 1

Standard Output in Dart:

In dart, there are two ways to display output in the console:

  1. Using print statement.
  2. Using stdout.write() statement.

Printing Output in two different ways –

filter_none

edit
close

play_arrow

link
brightness_4
code

import 'dart:io';
  
void main()
{
    // Printing in first way
    print("Welcome to GeeksforGeeks! // printing from print statement");
  
    // Printing in second way
    stdout.write("Welcome to GeeksforGeeks! // printing from stdout.write()");
}

chevron_right


Output:

Welcome to GeeksforGeeks! // printing from print statement
Welcome to GeeksforGeeks! // printing from stdout.write()

Note:
The print() statement brings the cursor to next line while stdout.write() don’t bring the cursor to the next line, it remains in the same line.

If the print statements are switched in the above program then:
Output:

Welcome to GeeksforGeeks! // printing from stdout.write()Welcome to GeeksforGeeks! // printing from print statement

Making a simple addition program –

filter_none

edit
close

play_arrow

link
brightness_4
code

import 'dart:io';
  
void main()
{
    print("-----------GeeksForGeeks-----------");
    print("Enter first number");
    int n1 = int.parse(stdin.readLineSync());
  
    print("Enter second number");
    int n2 = int.parse(stdin.readLineSync());
  
    // Adding them and printing them
    int sum = n1 + n2;
    print("Sum is $sum");
}

chevron_right


Input:

11
12

Output:

-----------GeeksForGeeks-----------
Enter first number
Enter second number
Sum is 23



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.