You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dart代码报错Unsupported operation,IntelliJ/AS控制台无法输入

解决Dart程序StdIO输入错误及IDE控制台只读问题

问题描述

编写了一段用于练习Dart基础的整数比较代码:

import 'dart:io';

void main() {
  print('Enter the first integer:');
  int? first = int.parse(stdin.readLineSync()!);

  print('Enter the second integer:');
  int? second = int.parse(stdin.readLineSync()!);

  if (first == second) {
    print('The two integers are equal.');
  } else {
    print('The two integers are not equal.');
  }
}

运行时出现错误:Error: Unsupported operation: StdIOUtils._getStdioInputStream,且IntelliJ/Android Studio的控制台处于只读状态,无法通过键盘输入内容。

解决方案

1. 调整IDE运行配置,启用控制台输入

  • 打开IntelliJ/Android Studio的Run/Debug Configurations(可通过右上角运行按钮旁的下拉菜单进入)
  • 找到当前的Dart运行配置,在配置页面中勾选Emulate terminal in output console选项
  • 点击Apply保存配置,重新运行程序

2. 优化代码的输入处理逻辑

原代码中的强制空断言(!)可能引发空指针异常,同时未处理非整数输入的情况,建议修改为更健壮的版本:

import 'dart:io';

void main() {
  print('Enter the first integer:');
  String? input1 = stdin.readLineSync();
  if (input1 == null) {
    print("输入无效,请重新运行程序");
    return;
  }
  int? first = int.tryParse(input1);
  if (first == null) {
    print("请输入有效的整数");
    return;
  }

  print('Enter the second integer:');
  String? input2 = stdin.readLineSync();
  if (input2 == null) {
    print("输入无效,请重新运行程序");
    return;
  }
  int? second = int.tryParse(input2);
  if (second == null) {
    print("请输入有效的整数");
    return;
  }

  if (first == second) {
    print('The two integers are equal.');
  } else {
    print('The two integers are not equal.');
  }
}

3. 命令行直接运行测试

如果IDE配置调整后仍有问题,可直接通过终端运行程序:

  • 打开终端,切换到代码所在的目录
  • 执行命令:dart run your_file_name.dart(替换为你的实际文件名)
  • 在终端中直接输入内容,验证代码逻辑是否正常

内容的提问来源于stack exchange,提问作者tiffjai91

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 13:01:23