dart - 为什么即使在打印字符时控制台也为空?

标签 dart

这是我在https://dartpad.dev上尝试过的简单的Dart代码。

void main(){  
  var numbers = new List(10);
  for(int i=0; i<=10; i++){
    numbers[i] = i;
  }
  print(numbers[1]);
  print("hello world");
}
为什么控制台为空?它不打印1hello world。此代码有什么问题?

最佳答案

您的代码有一个index out of range error,也就是说,当您使用i<=10时,您会得到它。

  1. Since, in your initialization of the List numbers, you have the length as 10, but due to i<=10, it will store the length of 11. Hence error.
  2. The code will go from 0,1,2,3,4,5,6,7,8,9,10, which is in total 11 in number. INDEX OUT OF RANGE ERROR
Uncaught Error: RangeError (index): Index out of range: index should be less than 10: 10
如果您不希望出现错误,则只需要按以下方式创建数据:i<10。否则,请使用i=1; i<11 => 1,2,3,4,5,6,7,8,9,10
void main(){  
  var numbers = new List(10);
  for(int i=0; i<10; i++){
    numbers[i] = i;
  }
  print(numbers[1]);
  print("hello world");
}
输出
1
hello world

关于dart - 为什么即使在打印字符时控制台也为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63337260/

相关文章:

dart - 在小部件或状态上保留最终字段?

Flutter 地理定位器插件 Geolocator().placemarkFromAddress() 和 Geolocator().placemarkFromCoordinates() 不起作用

dart - 如何使用 Flutter Cupertino 图标?

Dart/flutter : async behaviour of an Isolate's top level function

dart - 在 Dart 中必须 element.html ("") 出现在 body 标签的上下文中吗?

android - Flutter如何打开SD卡目录选择文件?

javascript - Dart 可以操作其他窗口中的 HTML 元素吗?

flutter - 代码错误,Flutter 中的 BuildContext

dart - 如何在一个 HTML 中执行两个 dart 代码

android - flutter 两个手指手势