Cannot modify a List Item/Element in Flutter - flutter

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.

Related

Flutter - duplicating an list of objects

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

Creating a structure within a structure with a dynamic name

I have large data sets which i want to work with in matlab.
I have a struct called Trail containing serveral structures called trail1, trail2 ...
which then contain several matrices. I now want to add another point to for instance trail1
I can do that with Trail.trail1.a2rotated(i,:) = rotpoint'; the problem is that i have to do it in a loop where the trail number as well as the a2rotated changes to e.g. a3rot...
I tired to do it like that
name ="trail"+num2str(z)+".a2rotated"+"("+i+",:)";
name = convertStringsToChars(name);
Trail.(name) = rotpoint'
But that gives me the error: Invalid field name: 'trail1.a2rotated(1,:)'.
Does someone have a solution?
The name in between brackets after the dot must be the name of a field of the struct. Other indexing operations must be done separately:
Trail.("trail"+z).a2rotated(i,:)
But you might be better off making trail(z) an array instead of separate fields with a number in the name.

Dart Hive use Set causing unknown type error

i would like to use set with hive. But it seems causing error: Unhandled Exception: HiveError: Cannot write, unknown type: _CompactLinkedHashSet<int>. Did you forget to register an adapter?
final Box box = Hive.box("NumberBox");
box.put("uniqueIds", <int>{});
var setList = box.get("uniqueIds");
setList.add(1);
setList.add(2);
setList.add(3);
According to the Hive documentation:
Hive supports all primitive types, List, Map, DateTime and Uint8List. If you want to store other objects, you have to register a TypeAdapter which converts the object from and to binary form.
Set is not supported out of the box, so you would need to handle that separately. The simplest way would be to convert your Set to a List when writing to the box, and convert that List to a Set when you read it back.

Delete specific value from firebase database using swift

Firebase Database
I tried using this bit of code but it doesn't seem to work. I take the name the user selects and store it in nameList.
Lets say I store Blake Wodruff in nameList[0].
How do I remove only that name?
var nameList = [String](repeating: "", count:100)
func remove() {
print(nameList[countAddNames])
let usernameRef = Database.database().reference().child("Candidate 1").child("alton").child(nameList[countAddNames]);
usernameRef.removeValue();
}
To write to or delete a node, you must specify its entire path. So to delete node 0 from your JSON, you'd do:
let usernameRef = Database.database().reference().child("Candidate 1").child("alton").child("0");
usernameRef.removeValue();
Or a bit shorter:
let usernameRef = Database.database().reference().child("Candidate 1/alton/0");
usernameRef.removeValue();
If you only know the name of the user you want to remove, you'll need to first look up its index/full path before you can remove it. If you have the data in your application already, you can do it in that code. Otherwise you may have to use a database query (specifically .queryOrderedByValue and .queryEqualToValue) to determine where the value exists in the database.
Also see: Delete a specific child node in Firebase swift
Once you remove a value from your JSON structure, Firebase may no longer recognize it as an array. For this reason it is highly recommended to not use arrays for the structure that you have. In fact, I'd model your data as a set, which in JSON would look like:
"alton": {
"Jake Jugg": true,
"Blake Wodruff": true,
"Alissa Sanchez": true
}
This would automatically:
Prevent duplicates, as each name can by definition only appear once.
Make removing a candidate by their name as easy as Database.database().reference().child("Candidate 1/alton/Jake Jugg").removeValue()
For more on this, also see my answer to Firebase query if child of child contains a value

Add rows in smartsheets using python

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