For example with the following List:
var originalList = ['follow', 'spread', 'operators.'];
When referencing it to make another List, why use the spread operator:
var spreadList = ['See? I', ...originalList]
Instead of without the spread operator:
var lonelyList = ['Actually I dont', originalList]
When the results for spreadList and lonelyList respectively are:
[See? I, follow, spread, operators.]
[Actually I dont, follow, spread, operators.]
CodePudding user response:
In this statement:
var spreadList = ['See? I', ...originalList]
With the spread operator you're inserting each individual String
element from originalList
as it's own element into spreadList
. The end result being a:
<String>['See? I', 'follow', 'spread', 'operators']
But in this statement:
var lonelyList = ['Actually I dont', originalList]
You're inserting the entire List as the second element. The end result being a:
<dynamic>['Actually I dont', ['follow', 'spread', 'operators']]
You probably didn't notice the second set of square brackets on the output
CodePudding user response:
Below is my code:
var originalList = ['follow', 'spread', 'operators.'];
var spreadList = ['See? I', ...originalList];
var lonelyList = ['Actually I dont', originalList];
print(spreadList);
print(lonelyList);
Here is my result:
I/flutter (26526): [See? I, follow, spread, operators.]
I/flutter (26526): [Actually I dont, [follow, spread, operators.]]
You can see that when using ...originalList
it removes the square brackets. This means that when using ...originalList
it adds multiple strings, but using originalList
adds a list.