I want to duplicate a list of objects. I want to do some things to to that dupliucate but not affect the original list.
Heres my code:
var tempHolidays = <Holiday>[];
tempHolidays = holidays;
tempHolidays[widget.index].start = widget.start;
tempHolidays[widget.index].end = widget.end;
The results i'm seeing would suggest the actions carried out on tempHolidays are mirroring on holidays? Is this possible or do I have a bug elsewhere?
What you are doing is just passing the references of the initial list to the second.
To duplicate a list use toList(), it return a different List object with the same element of the original list
tempHolidays = holidays.toList()
Related
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.
_items = []
..add(counter++)
..addAll(_items);
You can break this in following part
_items is a list hence it points to some memory address (lets say 0x1000 for simplicity)
You create a new list with []. Let's say it points to some other memory address (0x1500).
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.
When ..add(counter++) is done, List object pointing to memory address (0x1500) is returned and the list which was empty earlier contains single item.
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.
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.
I was just playing around with hash tables and I did this:
$C = #{color = [System.Collections.ArrayList]#(#{y = 'yellow'},#{y = 'blue'})}
and I ended up with finding I can do:
($C.color.y).Get(1) or ($C.color.y).Item(1) and it does exactly the same thing.
I also found even though the autocomplete doesn't show you can do this you can:
($C.color.y).Get(0).Contains('yellow')
($C.color.y).Get(1).Contains('blue')
The Get and Item methods appear to be interchangeable what is the difference between them?
I did check the hash codes they are the same.
($C.color.y).Get(1).GetHashCode() or ($C.color.y).Item(1).GetHashCode()
I have a list that is initialised as follows:
This is outside of main (so it is global as many other classes need to access it).
List mainMenuList = [];
It is then populated with shopping items.
so
mainMenuList.elementAt[counter].shoppingPrice;
or
print(mainMenuList.elementAt[counter].shoppingPrice);
gives '4.00';
but if I wanted to increment or change that element's value, I get an error:
flutter: Unsupported operation: read-only
So, for example, I'm trying to add supplements to it:
mainMenuList[listItemIndicator]['ItemPrice'] += supplementsList[supplementCounter]['SupplementPrice']);
I get the following error:
flutter: Unsupported operation: read-only
So even if I forget about supplements.. if I did something like:
mainMenuList[listItemIndicator]['ItemPrice'] = 1.00;
I get the same error
Many thanks!
You are trying to use key-value generic with List here or so the above code shows
mainMenuList[listItemIndicator]['ItemPrice'] = 1.00; //incorrect way of using List
You need to use either HashMap or LinkedMap for that.
How do I take a list of values, iterate through it to create the needed objects then pass that "list" of objects to the API to create multiple rows?
I have been successful in adding a new row with a value using the API example. In that example, two objects are created.
row_a = ss_client.models.Row()
row_b = ss_client.models.Row()
These two objects are passed in the add row function. (Forgive me if I use the wrong terms. Still new to this)
response = ss_client.Sheets.add_rows(
2331373580117892, # sheet_id
[row_a, row_b])
I have not been successful in passing an unknown amount of objects with something like this.
newRowsToCreate = []
for row in new_rows:
rowObject = ss.models.Row()
rowObject.cells.append({
'column_id': PM_columns['Row ID Master'],
'value': row
})
newRowsToCreate.append(rowObject)
# Add rows to sheet
response = ss.Sheets.add_rows(
OH_MkrSheetId, # sheet_id
newRowsToCreate)
This returns this error:
{"code": 1062, "errorCode": 1062, "message": "Invalid row location: You must
use at least 1 location specifier.",
Thank you for any help.
From the error message, it looks like you're missing the location specification for the new rows.
Each row object that you create needs to have a location value set. For example, if you want your new rows to be added to the bottom of your sheet, then you would add this attribute to your rowObject.
rowObject.toBottom=True
You can read about this location specific attribute and how it relates to the Python SDK here.
To be 100% precise here I had to set the attribute differently to make it work:
rowObject.to_bottom = True
I've found the name of the property below:
https://smartsheet-platform.github.io/smartsheet-python-sdk/smartsheet.models.html#module-smartsheet.models.row
To be 100% precise here I had to set the attribute differently to make it work:
Yep, the documentation isn't super clear about this other than in the examples, but the API uses camelCase in Javascript, but the same terms are always in snake_case in the Python API (which is, after all, the Pythonic way to do it!)
Consider the following code.
var items = from i in context.Items
select i;
var item = items.FirstOrDefault();
item.this = "that";
item.that = "this";
var items2 = from i in context.Items
where i.this == "that"
select i;
var data = items2.FirstOrDefault();
context.SaveChanges();
I'm trying to confirm that items2 will not include my modifications to item. In other words, items2's copy of item will not include the unsaved changes.
Have you tried it? =)
By default, your objects are being tracked and cached by the context, so that the objects in your second query actually do reflect changes in the first.
You may want to call context.Items.AsNoTracking() on the one of your two "items" to get the behavior you want.
Edit: Actually, this is a strange question. I just noticed that your items2 hasn't even hit the database yet, since you haven't called ToList() or FirstorDefault(). It remains an IQueryable that will hit the database after your code snippet and will therefore contain the changed value.
HOWEVER, if you call ToList() on items2, you'll encounter the caching scenario I outlined above.
In case of "var item" your query is executed the moment you used FirstOrDefault(). But for var items2 the query is still not executed. Now in your case result of items2 will always be affected by the updates you have done in the first query.
It will contain the modifications, only way to do is create a new context and query the new context.