Home > Software engineering >  How initializing newly created List with Literal syntax work with Cascade notation in Flutter or Dar
How initializing newly created List with Literal syntax work with Cascade notation in Flutter or Dar

Time:10-02

I have trouble understanding the bellow section of the code. Debugging the code don't help because things don't turnout as I expected. For example, in numberList = [] line, litteral syntax [ ] must initialize the list and make it empty but after execution of this line numberList has value?!

     // As I know this line should empty the list? but it doesn't?
numberList= []         
    // This line adds a value to numberList list and create a new object as a return? 
..add(1)   
   // How list numberList will be added to numberList again? This part is really confusing to me
..addAll(numberList);   

Complete Code:


    void main() {
      List<int> numberList=[10]; 
      numberList=[]..add(1)..add(2)..addAll(numberList);
      print(numberList);
    }

It seems adds 1 and 2 to [ ] (new list object), and then adds all numberList's items to [ ], and finally Assigns [1,2,10] list to numberList.

Is there a way to debug to see each addition? How can I define name to this so called anonymous or on the fly list?

CodePudding user response:

Interesting use-case.

I can decode the code for you, Along with cascading, scoping is also being used.

_items = []         
..add(counter  )   
..addAll(_items);

You can break this in following part

  1. _items is a list hence it points to some memory address (lets say 0x1000 for simplicity)
  2. You create a new list with []. Let's say it points to some other memory address (0x1500).
  3. Cascading operation means that an operation 'op' can be performed on an object but instead of returning of Type which is defined for 'op' (for example bool add(int t)), same object is returned on which cascading was performed. Here 'object' is returned instead of bool.
  4. When ..add(counter ) is done, List object pointing to memory address (0x1500) is returned and the list which was empty earlier contains single item.
  5. When ..addAll(_items) is performed, you are telling to add all the items from list at address (0x1000) to be added to list object which was returned earlier (i.e. list at 0x1500). So all items are added.
  6. One the operation is done, Right side of = operator is evaluated and list pointing to memory address 0x1500 is retuned which is assigned to variable written at left side (which is _items).

TL;DR: The all three lines are actually one statement.

I hope I cleared your doubt.

CodePudding user response:

In simple words, the cascading operator, that is "..", passes the result of the previous operation directly to the next operation.

For example:

var list=[]
..add(value)
..add(value2);

This statement first initialize empty list then add value to the list. After adding value it adds value2 to the list.

So this is generally the short form of:

var list=[];
list.add(value);
list.add(value2);

Here is the definition.

  • Related