Simple Dart Program With User Input from Keyboard

Basic example dart program taking use input from keyboard and printing the data. Here is a sample dart code that asks the user for age and displays the age given by the user.

This code covers the basic input output using dart code. User input and print dart features.

main.dart

import 'dart:io';

main(List<String> arguments) {
  print("What is your age");
  var age = stdin.readLineSync();
  print("Your age is ${age}");
}

Coding and running a dart program

We can create a dart file called main.dart and write the above code.

We can run this main.dart file in the terminal using the following code: dart main.dart

Dart Code Explanation

In the dart code stdin.readLineSync() function is used to read data from the user. The data the user inputs using the keyboard is stored to a variable age. Then the age is printed and displayed using the dart print function.

For printing a variable inside a string ${age} the variable age is wrapped inside ${ }

VS Code Editor plugin for running dart files

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *