> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/NormandoRamirezDelgado/6C-Febrero-2026/llms.txt
> Use this file to discover all available pages before exploring further.

# User Input

> Learn how to read user input from the console in Dart

## Introduction

In Dart, you can read user input from the console using the `stdin` object from the `dart:io` library. This is essential for creating interactive command-line applications.

## Reading String Input

To read text input from the user, use `stdin.readLineSync()`:

```dart theme={null}
import 'dart:io';

void main() {
  print("¿Cómo te llamas?");
  
  // El '?' es porque el valor puede ser nulo
  String? nombre = stdin.readLineSync();

  print("Hola, $nombre. ¡Mucho gusto!");
}
```

<Note>
  The return type is `String?` (nullable) because `readLineSync()` can return `null` if the input stream is closed or if an error occurs.
</Note>

## Reading Numeric Input

To read numeric values, you need to:

<Steps>
  <Step title="Read the input as a string">
    Use `stdin.readLineSync()` to get the user's input
  </Step>

  <Step title="Parse the string to a number">
    Use `int.parse()` or `double.parse()` to convert the string to a number
  </Step>

  <Step title="Handle the nullable value">
    Use the `!` operator to assert that the value is not null
  </Step>
</Steps>

### Reading Integers

```dart theme={null}
import 'dart:io';

void main() {
  print("Introduce tu edad:");
  
  // Leemos el texto y lo convertimos a entero
  int edad = int.parse(stdin.readLineSync()!);

  print("El año que viene tendrás ${edad + 1} años.");
}
```

### Complete Example

Here's a complete program that demonstrates reading both string and numeric input:

```dart theme={null}
import 'dart:io';

void main() {
  print("¿Cómo te llamas?");
  
  // El '?' es porque el valor puede ser nulo
  String? nombre = stdin.readLineSync();

  print("Hola, $nombre. ¡Mucho gusto!");

  //Introducir valores numericos int o double
  print("Introduce tu edad:");
  
  // Leemos el texto y lo convertimos a entero
  int edad = int.parse(stdin.readLineSync()!);

  print("El año que viene tendrás ${edad + 1} años.");
}
```

## Key Points

<CardGroup cols={2}>
  <Card title="Import Required" icon="file-import">
    Always import `dart:io` to access stdin
  </Card>

  <Card title="Nullable by Default" icon="question">
    `readLineSync()` returns `String?` which must be handled
  </Card>

  <Card title="Parse Numbers" icon="hashtag">
    Use `int.parse()` or `double.parse()` for numeric input
  </Card>

  <Card title="Null Assertion" icon="exclamation">
    Use `!` operator when you're sure the input won't be null
  </Card>
</CardGroup>

## Best Practices

<Warning>
  When using the `!` operator, make sure the input won't be null. Otherwise, your program will crash with a null reference error.
</Warning>

<Tip>
  For more robust applications, consider using try-catch blocks when parsing numeric input to handle invalid input gracefully.
</Tip>

```dart theme={null}
import 'dart:io';

void main() {
  print("Introduce tu edad:");
  
  try {
    int edad = int.parse(stdin.readLineSync()!);
    print("El año que viene tendrás ${edad + 1} años.");
  } catch (e) {
    print("Error: Por favor introduce un número válido");
  }
}
```
