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?
Interesting use-case.
I can decode the code for you, Along with cascading, scoping is also being used.
You can break this in following part
_itemsis a list hence it points to some memory address (lets say 0x1000 for simplicity)[]. Let's say it points to some other memory address (0x1500).bool add(int t)), same object is returned on which cascading was performed. Here 'object' is returned instead of bool...add(counter++)is done, List object pointing to memory address (0x1500) is returned and the list which was empty earlier contains single item...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.=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.