Aditya Karad
Aditya Karad

Reputation: 71

Error in using Stdin.readLineSync(); in dart

in vs code, i am getting this error in the basic input taking code from the user my complete code:

import 'dart:io';

void main(){
  stdout.write("Enter your name : ");
  var name = stdin.readLineSync();
  stdout.write(name);
}

Error in the compiler:

playground.dart:9:23: Error: Method not found: 'Stdin.readLineSync'.
  String name = Stdin.readLineSync();
                      ^^^^^^^^^^^^

Upvotes: 3

Views: 11183

Answers (9)

Ahmad Mithaloni
Ahmad Mithaloni

Reputation: 1

You can change this by going into File -> Preferences -> Settings. Here you go into Extensions -> Dart & Flutter. If you scroll down you can find "Dart: Cli Console". You can also just search for "Dart Cli console":

Upvotes: 0

chikega
chikega

Reputation: 155

Overall it's not good practice to use a null assertion operator ! whereby you're personally guaranteeing to the compiler that the variable in question will never have a null reference. In this case, it may be more prudent to write:

String? name = stdin.readLineSync();

https://api.dart.dev/stable/2.18.6/dart-io/dart-io-library.html

Upvotes: 0

Yavuz Erzurumlu
Yavuz Erzurumlu

Reputation: 31

Console applications aren't running on VSCode. I assume that your code is named "main.dart" You can use directly from the VSCode terminal:

dart run main.dart

Error on debug console: enter image description here

Running correct on terminal: enter image description here

My VSCode version is: 1.85.2

Upvotes: 2

Ahmed Taifour
Ahmed Taifour

Reputation: 21

you should write it -> stdout.writeln and also import the library for it, I amended the code for you below, and it works fine on VSCode

import 'dart:io';

void main(){
      stdout.writeln("Enter your name : ");
      var name = stdin.readLineSync();
      stdout.write(name);
    }

Upvotes: 0

sya mi
sya mi

Reputation: 1

The error you get with stdin.readLineSync() is due to null safety introduced in Dart 2.12. Just add (!)

var name = stdin.readLineSync()!;

Upvotes: 0

Veto scaletta
Veto scaletta

Reputation: 11

stdin.readLineSync()! use the '!' as I did below:

void main(){
  stdout.write("Enter your name : ");
  var name = stdin.readLineSync()!;
  stdout.write(name);
}

Upvotes: 1

aswin nk
aswin nk

Reputation: 1

if you were getting error like

"Getter not found.'stdin'"  

in VSCode check for the extension "dart" is installed on your VSCode then checkitout for run i.e dart run command

Upvotes: 0

aymanebenhima
aymanebenhima

Reputation: 51

You must add:

import 'dart:io';

before your main function

Upvotes: 4

salauddin23shumon
salauddin23shumon

Reputation: 165

To learn dart as console application you should use IntelliJ IDEA IDE.
This is the best IDE for dart.
vscode, dartpad does not support stdin stdout.

Upvotes: 4

Related Questions