Dart - Map with List as a value - flutter

I have this:
List<ForumCategories>? forumCategories;
var mainCats = List<ForumCategories>.empty(growable: true);
var subCats = <int, List<ForumCategories>>{};
Than this is what I do in order to populate it:
for (var i = 0; i < forumCategories!.length; i++) {
if (forumCategories![i].main == 0) {
subCats[forumCategories![i].slave]?.add(forumCategories![i]);
}
}
And than when I do:
var size = subCats.length;
print("SUBCATS: $size");
Output is always: SUBCATS: 0
This does not populate the map. Any idea why ?

because you're trying to add an item to a null List, see here:
if (forumCategories![i].main == 0) {
subCats[forumCategories![i].slave]?.add(forumCategories![i]);
}
subCats[forumCategories![i].slave] does not exist yet in your map, it's like you saying:
null.add(forumCategories![i]);
nothing will happen.
you need to initialize it by an empty List if it's null like this:
if (forumCategories![i].main == 0) {
(subCats[forumCategories![i].slave] ?? []).add(forumCategories![i]) // this will assign an empty list only the first time when it's null, then it adds elements to it.
}
now before adding elements to that list it will find the empty list to add.

Related

Flutter/Dart global List variable changed in a foreach loop is unchanged after the loop

I have a list declared outside a for loop and then I assign some values to this list inside that for loop and its value is updated when printed inside the loop but when I print it after the loop, it gives me an empty list.
List<List<String>> chunkSizeCollection(List<String> followedList) {
int counter = 0;
int ongoingCounter = 0;
bool isLessThanTen = false;
List<List<String>> returnAbleChunkedList = [];
List<String> midList = [];
log("in followedlist");
followedList.forEach((element) {
if (counter == 0) {
int difference = followedList.length - ongoingCounter;
if (difference < 10) {
// log("in difference if: $difference");
isLessThanTen = true;
}
}
midList.add(element);
counter++;
ongoingCounter++;
if (counter == 10 || (isLessThanTen && ongoingCounter == followedList.length)) {
returnAbleChunkedList.add(midList);
log("returnAbleChunkedList in counter 10 after adding new val is: $returnAbleChunkedList");
//above log works properly and prints the updated list
midList.clear();
counter = 0;
}
});
//this log on the other hand, returns an empty list
log("returnAbleChunkedList: $returnAbleChunkedList before return");
return returnAbleChunkedList;
}
The output:
[log] returnAbleChunkedList in counter 10 after adding new val is: [[KQTuEPllbmRrlBNvYgZ7oUXwtA63, OZUZOE10IzT8quUFoZbNxZOynU32, fCYIlYemCvbLTc7SpNHw6fCHrcm1, CbcLrtDNOdYZyC23FzEehOrJbKx2, FFvvVHCpPGNKUiXPQD34QdoPqH32, Gk09y59vSVXa1HNhzYvc6Atqnt53, JDO356z8urYQvuktJmc6eNUYqSm2, YesvvNI43gUVYPMfqhG4uRO5t6K2]]
[log] returnAbleChunkedList: [[]] before return
In this line:
returnAbleChunkedList.add(midList);
you add a reference to midList to your output list. If your input is longer than 10, you'll end up adding more than one reference to midList to the output list (i.e. you might now have 2). Subsequently, you clear midList so you now have an output list that contains 2 references to a list that you've now cleared, so you end up with a list containing 2 empty lists.
If, instead, you changed this to:
returnAbleChunkedList.add(midList.toList());
you'd add a copy of midList to your output list and your end up with:
[[a, b, c, d, e, f, g, h, i, j], [k]]
as you expected.
The problem is this:
returnAbleChunkedList.add(midList);
midList.clear();
You add the object midlist to the list and then you clear it. So the object you added will be emptied. You have to create a copy or add the elements of the list.
returnAbleChunkedList.add(midList.toList());
// or
returnAbleChunkedList.add(List.of(midList));
// or
returnAbleChunkedList.add([...midList]);

Flutter update a map value inside a list

Hello everyone i am trying to update a map value inside of a list if the value already exists then it should just update the quantity. The problem is that the code only works for the first entry and does not work for the rest of them.
here is my code.
for (var map in items) {
print(countItem.indexOf(_productIndex));
if (map['\'id\''] == '\'$_productIndex\'') {
var toRemove = countItem.indexOf(_productIndex);
items.removeAt(toRemove);
items.add({
'\'totalQuantity\'': count,
'\'type\'': '\'unit\'',
'\'id\'': '\'$_productIndex\'',
'\'price\'': count * int.parse(_productPrice),
'\'name\'': '\'$_productName\'',
});
} else {
items.add({
'\'totalQuantity\'': count,
'\'type\'': '\'unit\'',
'\'id\'': '\'$_productIndex\'',
'\'price\'': int.parse(_productPrice),
'\'name\'': '\'$_productName\'',
});
}
break;
}
Your using indexOf for the wrong variable. Change it from _productIndex to map.
for (var map in items) {
print(countItem.indexOf(map));
...

Sort table column with custom comparator

I have a table column which displays customers full names (first name + last name). However, I want to sort this column first by first name and then by last name. Therefore I added some comparator function in my controller:
_customNameComparator: function(value1, value2) {
// Separate all words of the full name
var aWordsName1 = value1.split(" ");
var aWordsName2 = value2.split(" ");
// Get the last and first names of the two names
var sFirstName1 = value1.substring(0, value1.lastIndexOf(" "));
var sLastName1 = aWordsName1[aWordsName1.length - 1];
var sFirstName2 = value2.substring(0, value1.lastIndexOf(" "));
var sLastName2 = aWordsName2[aWordsName2.length - 1];
// 0 values are equal
// -1 value1 smaller than value2
// 1 value1 larger than value2
if (sLastName1 === sLastName2) {
if (sFirstName1 === sFirstName2) {
return 0;
} else if (sFirstName1 > sFirstName2) {
return 1;
} else {
return -1;
}
} else if (sLastName1 > sLastName2) {
return 1;
} else {
return -1;
}
}
When the column Header is clicked, I try to call
var aSorter = [];
aSorter.push(new sap.ui.model.Sorter("FullName", bDescending, false, this._customNameComparator));
var oBinding = this.byId("tableTargetGroupDetails").getBinding("items");
oBinding.sort(aSorter);
The comparator does not work like this. The sorting is just as usual (by the full name). What do I do wrong?
And btw: I know that this can lead to some wrong sorting (e.g. for last names containing out of two or more words), but since it's "only" the sorting this is fine for me at the moment.
Unless your binding's operationMode is Client, your comparator will probably not work. You can set the mode where you do your binding using { parameters: { operationMode: 'Client' } }.

Trying to work with down() method from ExtJS 4.2.1

I am trying to find a specific element from my page using ExtJS 4 so I can do modifications on it.
I know its id so it should not be a problem BUT
-I tried Ext.getCmp('theId') and it just return me undefined
-I tried to use down('theId') method by passing through the view and I still get a nullresponse.
As I know the id of the element I tried again the two methods by setting manually the id and it didn't work neither.
Do these two methods not function?
How should I do?
Here is the concerned part of the code :
listeners: {
load: function(node, records, successful, eOpts) {
var ownertree = records.store.ownerTree;
var boundView = ownertree.dockedItems.items[1].view.id;
var generalId = boundView+'-record-';
// Add row stripping on leaf nodes when a node is expanded
//
// Adding the same feature to the whole tree instead of leaf nodes
// would not be much more complicated but it would require iterating
// the whole tree starting with the root node to build a list of
// all visible nodes. The same function would need to be called
// on expand, collapse, append, insert, remove and load events.
if (!node.tree.root.data.leaf) {
// Process each child node
node.tree.root.cascadeBy(function(currentChild) {
// Process only leaf
if (currentChild.data.leaf) {
var nodeId = ""+generalId+currentChild.internalId;
var index = currentChild.data.index;
if ((index % 2) == 0) {
// even node
currentChild.data.cls.replace('tree-odd-node', '')
currentChild.data.cls = 'tree-even-node';
} else {
// odd node
currentChild.data.cls.replace('tree-even-node', '')
currentChild.data.cls = 'tree-odd-node';
}
// Update CSS classes
currentChild.triggerUIUpdate();
console.log(nodeId);
console.log(ownertree.view.body);
console.log(Ext.getCmp(nodeId));
console.log(Ext.getCmp('treeview-1016-record-02001001'));
console.log(ownertree.view.body.down(nodeId));
console.log(ownertree.view.body.down('treeview-1016-record-02001001'));
}
});
}
}
You can see my console.log at the end.
Here is what they give me on the javascript console (in the right order):
treeview-1016-record-02001001
The precise id I am looking for. And I also try manually in case...
h {dom: table#treeview-1016-table.x-treeview-1016-table x-grid-table, el: h, id: "treeview-1016gridBody", $cache: Object, lastBox: Object…}
I checked every configs of this item and its dom and it is exactly the part of the dom I am looking for, which is the view containing my tree. The BIG parent
And then:
undefined
undefined
null
null
Here is the item I want to access:
<tr role="row" id="treeview-1016-record-02001001" ...>
And I checked there is no id duplication anywhere...
I asked someone else who told me these methods do not work. The problem is I need to access this item to modify its cls.
I would appreciate any idea.
You are looking for Ext.get(id). Ext.getCmp(id) is used for Ext.Components, and Ext.get(id) is used for Ext.dom.Elements. See the docs here: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext-method-get
Ok so finally I used the afteritemexpand listener. With the ids I get the elements I am looking for with your Ext.get(id) method kevhender :).
The reason is that the dom elements where not completely loaded when I used my load listener (it was just the store) so the Ext.get(id) method couldn't get the the element correctly. I first used afterlayout listener, that was correct but too often called and the access to the id was not so easy.
So, here is how I did finally :
listeners: {
load: function(node, records, successful, eOpts) {
var ownertree = records.store.ownerTree;
var boundView = ownertree.dockedItems.items[1].view.id;
var generalId = boundView+'-record-';
if (!node.tree.root.data.leaf) {
// Process each child node
node.tree.root.cascadeBy(function(currentChild) {
// Process only leaf
if (currentChild.data.leaf) {
var nodeId = ""+generalId+currentChild.internalId;
var index = currentChild.data.index;
if ( (index % 2) == 0 && ids.indexOf(nodeId) == -1 ) {
ids[indiceIds] = nodeId;
indiceIds++;
}
console.log(ids);
}
});
}
},
afteritemexpand: function( node, index, item, eOpts ){
/* This commented section below could replace the load but the load is done during store loading while afteritemexpand is done after expanding an item.
So, load listener makes saving time AND makes loading time constant. That is not the case if we just consider the commented section below because
the more you expand nodes, the more items it will have to get and so loading time is more and more important
*/
// var domLeaf = Ext.get(item.id).next();
// for ( var int = 0; int < node.childNodes.length; int++) {
// if (node.childNodes[int].data.leaf && (int % 2) == 0) {
// if (ids.indexOf(domLeaf.id) == -1) {
// ids[indiceIds] = domLeaf.id;
// indiceIds++;
// }
// }
// domLeaf = domLeaf.next();
// }
for ( var int = 0; int < ids.length; int++) {
domLeaf = Ext.get(ids[int]);
if (domLeaf != null) {
for ( var int2 = 0; int2 < domLeaf.dom.children.length; int2++) {
if (domLeaf.dom.children[int2].className.search('tree-even-node') == -1){
domLeaf.dom.children[int2].className += ' tree-even-node';
}
}
}
}
},
With ids an Array of the ids I need to set the class.
Thank you for the method.

Ext.form.Basic how to reset fields when trackResetOnLoad true?

I got a Ext.form.Basic with the trackResetOnLoad:true config.
When I call reset() on a field it gets its values from the form setValues() method.
How do I reset my fields now?
When I just do field.setValue('') the form marks it as invalid because the field is required.
Thanks in advance.
You have to manually reset all of the originValues of all fields (and some other)
This snipped will do this
var items = form.getForm().getFields().items,
i = 0,
len = items.length;
for(; i < len; i++) {
var c = items[i];
c.value = '';
if(c.mixins && c.mixins.field && typeof c.mixins.field['initValue'] == 'function'){
c.mixins.field.initValue.apply(c);
c.wasDirty = false;
}
}
working example
What works for me is to take a copy of the values just after the form is created using something like var originalValues = myForm.getFieldValues(); then I can later restore those values using myForm.setValues(originalValues); instead of calling myForm.reset(...);