You are currently viewing Dart: User Input

Dart: User Input

Imagine you are having a conversation with a robot. You ask it a question, and it replies. But what if the robot could never hear you? That wouldn’t be fun, right? In programming, we need a way to let our programs “listen” to us. This is called user input.

Dart, like other programming languages, allows us to take input from users so that our programs can respond. In this article, we’ll learn how to make Dart programs that can ask questions, understand numbers, and even work with multiple pieces of information at once!

Reading User Input in Dart

In Dart, we can make our programs listen to us by using something called stdin.readLineSync(). Think of it as a microphone that lets your program hear what you type.

But before we can use it, we need to import a special tool called dart:io. This tool helps Dart talk to the computer’s input and output systems.

Example: Asking for a Name

Let’s write a small program where Dart asks for your name and greets you:

import 'dart:io';

void main() {

  print("What is your name?");
  String? name = stdin.readLineSync(); 

  print("Hello, $name! Nice to meet you.");

}

What’s Happening Here?

  1. print("What is your name?"); – The program asks a question.
  2. stdin.readLineSync(); – It listens for what you type and stores it in name.
  3. print("Hello, $name! Nice to meet you."); – It uses your name to greet you.

Try running this! When you type your name, the program will say hello.

Handling Different Data Types

Right now, our program only understands words (text). But what if we want to ask for numbers?

When we type something in, Dart treats it as text (a string). If we want to use numbers, we must convert the text into a number.

Example: Adding Two Numbers

import 'dart:io';

void main() {

  print("Enter the first number:");
  String? input1 = stdin.readLineSync();

  print("Enter the second number:");
  String? input2 = stdin.readLineSync();

  int num1 = int.parse(input1!);
  int num2 = int.parse(input2!);

  int sum = num1 + num2;
  print("The sum of $num1 and $num2 is $sum.");

}

What’s Happening Here?

  1. The program asks for two numbers.
  2. stdin.readLineSync() captures what you type.
  3. int.parse(input1!) converts the text into a number.
  4. The program adds the numbers and shows the result.

What If We Type Something Wrong?

If you type words instead of numbers, the program will crash! We can fix this using try-catch, which acts like a safety net.

import 'dart:io';

void main() {

  print("Enter a number:");
  String? input = stdin.readLineSync();

  try {
    int number = int.parse(input!);
    print("You entered: $number");
  } catch (e) {
    print("Oops! That’s not a number.");
  }

}

Now, if someone types letters instead of numbers, the program won’t crash—it will just show a friendly message!

Working with Multiple Inputs

Sometimes, we want to take many inputs at once. Imagine you are making a program that asks for three numbers at the same time. We can do that using split().

Example: Entering Multiple Numbers

import 'dart:io';

void main() {

  print("Enter three numbers separated by spaces:");
  String? input = stdin.readLineSync();

  List<String> parts = input!.split(" ");

  int num1 = int.parse(parts[0]);
  int num2 = int.parse(parts[1]);
  int num3 = int.parse(parts[2]);

  int sum = num1 + num2 + num3;
  print("The total sum is $sum.");

}

What’s Happening Here?

  1. The user enters numbers like: 3 5 7.
  2. split(" ") breaks the input into separate pieces.
  3. Each piece is converted into a number.
  4. The program adds them together and shows the total.

Conclusion

You now have the skills to interact with your Dart programs through user input. In this guide, you learned how to:

  • Obtain user input using stdin.readLineSync().
  • Convert text input into numerical values.
  • Handle errors to prevent program crashes.
  • Process multiple inputs simultaneously.

You can now experiment with creating projects like a calculator, quiz, or even a chatbot. The potential applications are limitless.

Leave a Reply