flutter语法中三点的含义 [英] Meaning of Triple Dots [...] in Flutter Syntax
问题描述
有人可以澄清一下Flutter中"..."的含义和用法吗?
Can someone please clarify what is the meaning and usage of "..." in Flutter?
我想了解Flutter语法中使用的三点".经过一番阅读后,我发现我要找的词是传播".
I wanted to learn about "triple dots" used in Flutter syntax. After some reading I found out that the word I was looking for was "spreading".
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'.
If I didn't have the ...
right before the children,
it will give an error The element type 'List<Widget>' can't be assigned to the list type 'Widget'.
我只是认为有人应该发布有关此问题的信息. flutter语法中的"..."是什么?是什么意思?
I just thought that someone should post a question about it. What is "..." in flutter syntax? What does it mean?
推荐答案
Dart 2.3引入了散布运算符(...)和可识别null的散布运算符(...?),它们提供了一种插入多个简明格式的简洁方法元素添加到集合中.
Dart 2.3 introduced the spread operator (...) and the null-aware spread operator (...?), which provide a concise way to insert multiple elements into a collection.
例如,您可以使用传播运算符(...)将列表的所有元素插入另一个列表:
For example, you can use the spread operator (...) to insert all the elements of a list into another list:
var list = [1, 2, 3];
var list2 = [0, ...list];
assert(list2.length == 4);
如果扩展运算符右边的表达式可能为null,则可以使用可识别null的扩展运算符(...?)来避免出现异常:
If the expression to the right of the spread operator might be null, you can avoid exceptions by using a null-aware spread operator (...?):
var list;
var list2 = [0, ...?list];
assert(list2.length == 1);
For more details and examples of using the spread operator, see the spread operator proposal.
这篇关于flutter语法中三点的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!