Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the usage of three dots (...) in dart List?

Tags:

flutter

dart

I have my code written like this but it gives an error saying:

Error: A value of type 'List' can't be assigned to a variable of type 'Widget'.

Column(
  children: [
    Question(
      questions[_questionIndex]['questionText'],
    ),
    ...(questions[_questionIndex]['answers'] as List<String>)
        .map((answer) {
      return Answer(_answerQuestion, answer);
    }).toList()
  ],
)
like image 385
Farwa Avatar asked Sep 12 '19 06:09

Farwa


People also ask

What is dot dot in Dart?

In the Dart programming language, the dot dot ( .. ) operator is also interpreted as "cascade notation". It allows you to not repeat the same target if you want to call several methods on the same object.

What does 2 dots mean in flutter?

You can use the "double dot" to call functions on objects and access properties. This "operator" is simply used to make your code cleaner and concise. It often saves you from having to create temporary variables.

What is the use of spread operator in Dart?

In Dart, Spread Operator (…) and Null-aware Spread Operator (…?) are used for inserting multiple elements in a collection like Lists, Maps, etc. Syntaxes: Spread operator.


3 Answers

Dart 2.3 introduce Spread operator (…)

Reference link : 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]
like image 74
Amit Prajapati Avatar answered Nov 02 '22 20:11

Amit Prajapati


Dart 2.3 comes with the spread operator (...) and the null-aware spread operator (...?), which allows us to add multiple elements in Collections.

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

For Flutter, we can use this inside column

   Column(
          children: [
            ...values.map((value) {
              return Text(value);
            }),
          ],
        ),

Output:

enter image description here

like image 20
Jitesh Mohite Avatar answered Nov 02 '22 19:11

Jitesh Mohite


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

Results

[1, 2, 3, [20, 30, 40]]
[1, 2, 3, 20, 30, 40]
like image 29
هيثم Avatar answered Nov 02 '22 19:11

هيثم