【发布时间】:2019-10-14 09:08:48
【问题描述】:
当foo 已经是const 值时,为什么会出现错误?
const foo = const [10, 20];
const bar = foo[0] * 2; // error: const variables must be initialized with a constant value.
【问题讨论】:
标签: dart
当foo 已经是const 值时,为什么会出现错误?
const foo = const [10, 20];
const bar = foo[0] * 2; // error: const variables must be initialized with a constant value.
【问题讨论】:
标签: dart
这是因为虽然用于创建第二个常量的变量是常量,但您还使用了运算符 []——它不是编译时常量。
所以虽然你可以这样做:
const a = 42;
const b = a * 3;
你不能这样做:
const array = [42];
const b = a[0];
【讨论】: