谁能解释一下“...”在 Flutter 中的含义和用法?
我想了解 Flutter 语法中使用的“三点”。阅读之后,我发现我要找的词是“传播”。
Widget _build() {
List<Widget> children = [
Text("first child"),
Text("second child"),
Text("third child"),
];
return Column(
children: <Widget>[
...children,
Text("fourth child"),
],
);
}
如果我没有 ...
就在 children,
之前它会给出错误 The element type 'List<Widget>' can't be assigned to the list type 'Widget'.
我只是认为应该有人发布有关它的问题。 flutter 语法中的“...”是什么?什么意思?
最佳答案
Dart 2.3 引入了扩展运算符 (...) 和 null 感知扩展运算符 (...?),它们提供了一种将多个元素插入集合的简洁方法。
例如,您可以使用扩展运算符 (...) 将一个列表的所有元素插入到另一个列表中:
var list = [1, 2, 3];
var list2 = [0, ...list];
assert(list2.length == 4);
如果扩展运算符右侧的表达式可能为 null,您可以通过使用 null-aware 扩展运算符 (...?) 来避免异常:
var list;
var list2 = [0, ...?list];
assert(list2.length == 1);
有关使用扩展运算符的更多详细信息和示例,请参阅 spread operator proposal .
关于flutter - Flutter 语法中三点 [...] 的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57194452/