Dart – Read an Integer from Console

To read an integer from console in Dart, first read a line from console using stdin.readLineSync() method, and then convert this line (string) into an integer using int.parse() method.

Syntax

The syntax to read an integer from console is

</>
Copy
int.parse(stdin.readLineSync()!)

To use stdin.readLine() method, import 'dart:io' package.

Examples

In the following program, we read an integer from console into variable N, and then print it back to console.

main.dart

</>
Copy
import 'dart:io';

void main() {
  print('Enter N');
  int N = int.parse(stdin.readLineSync()!);
  print('You entered $N');
}

Output

Enter N
123456
You entered 123456

Conclusion

In this Dart Tutorial, we learned how to read an integer from console in Dart language, with examples.