How to get input from users in Dart

Dart is a client optimized programming language that can be used for apps on multiple platforms. It is based on object oriented programming and it can be compiled to JavaScript or native code.

How to take input in Dart

To take input from a user in Dart, you need to import the dart:io library. The input is taken through the console using the .readLineSync() function.

Code

In the code below, enter your name in stdin column first and then press the run button:

import 'dart:io';
void main()
{
String name = stdin.readLineSync();
print("Hello, $name");
}

Enter the input below

This was how you can take a string as input in the console. Below, we will see how we can take an integer value as input in the console:

import 'dart:io';
void main()
{
int number = int.parse(stdin.readLineSync());
print("The number is $number");
}

Enter the input below

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources