Dart 列表负索引不起作用

Dev*_*wal 2 dart

Dart 的List结构似乎不支持负索引。这背后的原因是什么?我曾经使用过的所有其他语言都支持这一点。为什么 dart 决定排除这样一个基本结构?

以下代码 -

void main() {
    List<String> x = ["foo", "bar"];
    print(x[-1]);
}
Run Code Online (Sandbox Code Playgroud)

产生——

Uncaught exception:
RangeError (index): Index out of range: index must not be negative: -1
Run Code Online (Sandbox Code Playgroud)

Lil*_*oat 7

要打印最后一个元素,您可以简单地使用:

print(x.last);


Gün*_*uer 5

Dart 不支持负索引。

要访问相对于末尾的元素,您可以使用计算索引

print(x[x.length - 1])
Run Code Online (Sandbox Code Playgroud)

您可以在https://github.com/dart-lang/sdk 中创建功能请求以获取语言设计者的反馈。