Search code examples
dartinputstdinreadlinenosuchmethoderror

how to solve this error in dart language: NoSuchMethodError : method not found: 'readLineSync'


im new to dart language and when i want to take user input for a command line simple number guessing game using "stdin.readLineSync()", i get this error . my code is:

import 'dart:math';
import 'dart:io';

void main() {
  int guess;
  Random rand = new Random(); //create a random number generator
  int answer = rand.nextInt(100); //gets a random integer from 0 to 99
  do {
    print("Enter your guess:");
    String temp = stdin.readLineSync(); //read in from the keyboard
    guess = int.parse(temp); //convert String to integer
    if (guess < answer) {
      print("Too low!");
    } else if (guess > answer) {
      print("Too high!");
    }
  } while (guess != answer);
  print("You got it!");
}

the error is for this line: String temp = stdin.readLineSync(); //read in from the keyboard and i get this notification:

Unhandled exception:
NoSuchMethodError : method not found: 'readLineSync'
Receiver: Instance of '_SocketInputStream@0x1da10ec4'
Arguments: []
#0      Object._noSuchMethod (dart:core-patch:1360:3)
#1      Object.noSuchMethod (dart:core-patch:1361:25)

Solution

  • That is a extremely old version of Dart you are using since Dart Editor has been removed a long time ago. Please use latest version of Dart (2.10.3) and use either IntelliJ (dart.dev/tools/jetbrains-plugin) or VS Code (dart.dev/tools/vs-code). thanks to @julemand101.