如何使用Dart语言解决此错误:NoSuchMethodError:未找到方法:'readLineSync'

问题描述

我是dart语言的新手,当我想使用“ stdin.readLineSync()”为命令行简单数字猜谜游戏输入用户输入时,出现此错误。 我的代码是:

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!");
}

此行的错误是:String temp = stdin.readLineSync(); //从键盘读入 我收到此通知:

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)

解决方法

这是您使用的Dart的一个非常老的版本,因为Dart编辑器很久以前就被删除了。请使用Dart(2.10.3)的最新版本,并使用IntelliJ(dart.dev/tools/jetbrains-plugin)或VS Code(dart.dev/tools/vs-code)。 感谢@ julemand101。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...