DartPad 允许字符串迭代

DartPad allows string iteration

我想知道为什么下面的代码在 DartPad 上打印 test 的每个字母,并在 [=18] 上抛出异常=]终端机。我的意思是,Dart 中的字符串不可迭代,所以我不明白这在 DartPad 上是如何工作的;我也希望那里有一个例外。有什么想法吗?

void main() {
  var test = 'test';
  for (var t in test) {
    print(t);
  }
}

我用 Dart 1.14.0 测试了这个,但这在以前的版本中也发生过。

我想这是因为 Dart 被转换为 JavaScript 并且 JavaScript 支持遍历字符串并且可能出于性能原因没有额外的检查来阻止它。

恕我直言,什么是错误,分析器不显示警告,即使 test 明确键入为 String

void main() {
  String test = 'test';
  for (var t in test) {
    print(t);
  }
}

当我启用强模式时,我收到警告。

my_project/.analysis_options

analyzer:
  strong-mode: true

Type check failed: test (String) is not of type Iterable

两者都

var test = 'test';
// or 
String test = 'test';