飞镖列表中三个点(...)的用法是什么?

Far*_*rwa 17 dart flutter

我的代码是这样写的,但它给出了一个错误说:

错误:无法将“List”类型的值分配给“Widget”类型的变量。

Column(
  children: [
    Question(
      questions[_questionIndex]['questionText'],
    ),
    ...(questions[_questionIndex]['answers'] as List<String>)
        .map((answer) {
      return Answer(_answerQuestion, answer);
    }).toList()
  ],
)
Run Code Online (Sandbox Code Playgroud)

Ami*_*ati 17

Dart 2.3 引入 Spread 操作符(…)

参考链接:https : //medium.com/flutter-community/whats-new-in-dart-2-3-1a7050e2408d

    var a = [0,1,2,3,4];
    var b = [6,7,8,9];
    var c = [...a,5,...b];

    print(c);  // prints: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)


هيث*_*يثم 13

var x = [20,30,40];
var list = [1,2,3,x];

// Using the spread operator, adds the items individually not as a list
var separatedList = [1,2,3,...x];

print(list); // length = 4
print(separatedList); // length = 6
Run Code Online (Sandbox Code Playgroud)

结果

[1, 2, 3, [20, 30, 40]]
[1, 2, 3, 20, 30, 40]
Run Code Online (Sandbox Code Playgroud)


jit*_*555 5

Dart 2.3 带有spread operator (...)null-aware spread operator (...?),它允许我们在集合中添加多个元素。

  List<String> values1 = ['one', 'two', 'three'];
  List<String> values2 = ['four', 'five', 'six'];
  var output = [...values1,...values2];
  print(output); // [one, two, three, four, five, six]
Run Code Online (Sandbox Code Playgroud)

对于 Flutter,我们可以在列内使用它

   Column(
          children: [
            ...values.map((value) {
              return Text(value);
            }),
          ],
        ),
Run Code Online (Sandbox Code Playgroud)

输出:

在此处输入图片说明


Far*_*rwa 0

我已经弄清楚我的问题了。我的flutter SDK没有升级。运行flutter doctor命令并整理丢失的更新。现在语法似乎运行良好。

flutter upgrade

Dart 版本(必需)>= 2.3

重新启动 IDE(如果问题仍然存在)