Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of Triple Dots [...] in Flutter Syntax

Tags:

syntax

flutter

Can someone please clarify what is the meaning and usage of "..." in 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"),
    ],
  );
}

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'.

I just thought that someone should post a question about it. What is "..." in flutter syntax? What does it mean?

like image 456
aLL Avatar asked Jul 25 '19 04:07

aLL


People also ask

What do 3 dots mean in flutter?

Since version 2.3, Dart adds a new operator called spread which uses three dots ( ... ) notations. It can be used to extend the elements of a Collection .

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 does two dots mean in Dart?

Double dots(..)It allows you to not repeat the same target if you want to call several methods on the same object.


1 Answers

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);

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.

like image 59
Hassan Saleh Avatar answered Sep 24 '22 15:09

Hassan Saleh