User Input in Dart

User Input In Dart

Instead of writing hard-coded values, you can give input to the computer. It will make your program more dynamic. You need to import package import 'dart:io'; for user input.

Info

Note: You won’t be able to take input from users using dartpad. You need to run program from your computer.

String User Input

They are used for storing textual user input. If you want to keep values like somebody’s name, address, description, etc., you can take string input from the user.

import 'dart:io';

void main() {
  print("Enter name:");
  String? name  = stdin.readLineSync();
  print("The entered name is ${name}");
}
Show Output

Integer User Input

If you want to get a numeric value from the user without the decimal point, you can use integer input. E.g. 10, 100, -800 etc.

import 'dart:io';

void main() {
  print("Enter number:");
  int? number = int.parse(stdin.readLineSync()!);
  print("The entered number is ${number}");
}
Show Output

Floating Point User Input

If you want to get a numeric value from the user with the decimal point, you can use float input. E.g. 10.5, 100.5, -800.9 etc.

import 'dart:io';

void main() {
  print("Enter floating number:");
  double number = double.parse(stdin.readLineSync()!);
  print("The entered num is $number");
}
Show Output