Add an another attribute to an existing List in Flutter - list

I have been trying many ways to add an isChecked: false attribute to every list inside the _filterOptions List in my Flutter Project. I couldn't find any solution to this. If there is any solution I am happy to see it.
List<dynamic> _filters = [];
List<List<dynamic>> _filterOptions =[];
var filters =
await json.decode(data)["getProductsByStore"]["aggregations"];
for(int i = 0; i < filters.length; i++){
_filters.add(filters[i]["label"]);
_filterOptions.add(filters[i]["options"]);
}
//print(filters[0]["options"]);
//print(_filters);
print(_filterOptions);
I am getting this kind of nested list when printed and I want to add the attribute to every list.
as an example:
[[{count: 3, label: 30-40, value: 30_40, isChecked:false}], [{count: 2, label: Bagged , value: 50, isChecked:false},{...},{...}...]]

Create Model class with your data and bool isChecked
class ModelClass{
String? label;
int? count;
List? options;
bool? isChecked;
ModelClass({this.label, this.count , this.options, this.isChecked = false});
ModelClass.fromJson(Map<String, dynamic> json){
label = json['label'];
count = json['count'];
options = json['options'];
}
}
then you can add data like below code
List<ModelClass> list = [];
void function()async{
List<dynamic> _filters = [];
List<List<dynamic>?> _filterOptions =[];
var filters =
await json.decode(data)["getProductsByStore"]["aggregations"];
for(int i = 0; i < filters.length; i++){
ModelClass modelClass = ModelClass.fromJson(filters[i]);
list.add(modelClass);
_filters.add(modelClass.label);
_filterOptions.add(modelClass.options);
}
print(list[0].options);
print(list[0].isChecked);
}
if you want to modify isChecked for some specific index
list[0].isChecked = false;

Just add these lines.
class YourWrapperClass {
YourWrapperClass({this.filterOptions = const [], this.isChecked = false});
List<List<dynamic>> filterOptions;
bool isChecked;
}
YourWrapperClass filterWrapper = YourWrapperClass(filterOptions: _filterOptions, isChecked: false);
And use this wrapper class object whenever ya need to access _filterOptions.
But, I would more rather recommend using the WrapperClass on the list elements, like so.
class YourWrapperClass {
YourWrapperClass({this.filterOption = const [], this.filter="", this.isChecked = false});
List<dynamic> filterOption;
dynamic filter;
bool isChecked;
}
List<YourWrapperClass> list = [];
var filters =
await json.decode(data)["getProductsByStore"]["aggregations"];
for(int i = 0; i < filters.length; i++){
list.add(YourWrapperClass(filterOption:filters[i]["options"], filter:filters[i]["label"], isChecked=false));
_filterOptions.add(filters[i]["options"]);
}

Related

Trying to fill in a List of class

I'm really sorry I'm such a beginner...
At the end I just have a list of 8 TimeCardDayStrip with the same name/position when it'd supposed to be the 8 different names/roles.
I don't understand what I'm doing wrong here:
thank you very much
'''
class TimeCardDayStrip {
String name, position;
DateTime day;
#override
String toString(){
return '{ $this.name, $this.position }';
}
}
void main() {
var tcds = TimeCardDayStrip();
var listOfTcds = [];
List<String> names, roles;
names = ["Michael", "Gunnell", "Byrne", "Aspromonte", "Davis", "Adam Jordan", "Mirko"];
roles = ["Director", "Vice", "President", "1ST", "KEY 2ND", "2ND", "BASECAMP PA", "PA", " PA", "Add'l PA"];
for (int i = 0; i < names.length; i++) {
tcds.name = names[i];
tcds.position = roles[i];
listOfTcds.add(tcds);
// checking the list as it creates
print(listOfTcds[i].name+' is '+listOfTcds[i].position);
}
//print the list of card to check
print('print the list of time card to check');
for (int x = 0; x < listOfTcds.length; x++){
print (listOfTcds.elementAt(x).name);
}
}
'''
If you just want to print out each name:
Put this within the loop (you should create new object each loop instead of updating the same one)
for (int i = 0; i < names.length; i++) {
var tcds = TimeCardDayStrip();
tcds.name = names[i];
tcds.position = roles[i];
listOfTcds.add(tcds);
// checking the list as it creates
print('${listOfTcds[i].name} is ${listOfTcds[i].position}');
}
Then later
listOfTcds[x].name
If you want to display stuff:
#override
Widget build(BuildContext ctx) {
return ListView.builder(
itemCount: listOfTcds.length,
itemBuilder: (context, index) {
return CustomTile(item: listOfTcds[index]);
},
);
}
I believe you need to declare the tcds as Tcds = new TimecardDayStrip() and not just tcds = TimeCardDayStrip() or you are just changing the same object and not creating a new instance.

How to add dynamic values to field injections list with custom trigger to camunda properties panel?

I have two questions here
Is it possible to add dynamic lists values to field injection list input ?
Can I create a trigger for this so this can be initiated from any other input selection say a class selection will populate all fields
I was just looking into FieldInjection.js whether that can be extented for the same
Can someone please provide a hint or direction for this ?
Thanks.
For anyone interested in the answer, I was able to achieve the above goal by changing the set function of the Java Class select input as folllowing
few imports
var extensionElementsHelper = require('../../../../helper/ExtensionElementsHelper'),
elementHelper = require('../../../../helper/ElementHelper')
var CAMUNDA_FIELD_EXTENSION_ELEMENT = 'camunda:Field';
function getExtensionFields(bo) {
return bo && extensionElementsHelper.getExtensionElements(bo, CAMUNDA_FIELD_EXTENSION_ELEMENT) || [];
}
then changing the set function to create extension element and push the field values as :
set: function(element, values, node) {
var bo = getBusinessObject(element);
var type = getImplementationType(element);
var attr = getAttribute(type);
var prop = {}
var commands = [];
prop[attr] = values.delegate || '';
var extensionElements = getExtensionFields(bo);
//remove any extension elements existing before
extensionElements.forEach(function(ele){
commands.push(extensionElementsHelper.removeEntry(getBusinessObject(element), element, ele));
});
if(prop[attr] !== ""){
var extensionElements = elementHelper.createElement('bpmn:ExtensionElements', { values: [] }, bo, bpmnFactory);
commands.push(cmdHelper.updateBusinessObject(element, bo, { extensionElements: extensionElements }));
var arrProperties = ["private org.camunda.bpm.engine.delegate.Expression com.cfe.extensions.SampleJavaDelegate.varOne","private org.camunda.bpm.engine.delegate.Expression com.cfe.extensions.SampleJavaDelegate.varTwo"]
var newFieldElem = "";
arrProperties.forEach(function(prop){
var eachProp = {
name:"",
string:"",
expression:""
}
var type = prop.split(" ")[1].split(".").reverse()[0];
var val = prop.split(" ")[2].split(".").reverse()[0];
eachProp.name = val;
if( type == "String"){
eachProp.string = "${" + val +" }"
}else if( type == "Expression"){
eachProp.expression = "${" + val +" }"
}
newFieldElem = elementHelper.createElement(CAMUNDA_FIELD_EXTENSION_ELEMENT, eachProp, extensionElements, bpmnFactory);
commands.push(cmdHelper.addElementsTolist(element, extensionElements, 'values', [ newFieldElem ]));
});
}
commands.push(cmdHelper.updateBusinessObject(element, bo, prop));
return commands;
}
Cheers !.

Strange issue on reduce function

I always have reduce issue on practicing, like following map and reduce, if I add more document or add more field to emit in the map and reduce like following. it will return values as [], not sure what occur this?
problemNumber : doc.problemNumber,
UserId: idv,
event : doc.event
......
Map
function(doc) {
if(doc.event){
var idv = null;
for (var idu in doc.Data.users){
if (doc.eventData.users[idu].userTypeCode == "M"){
idv = doc.Data.users[idu].UserId;
}
}
var newDoc = {
problemNumber : doc.problemNumber,
UserId: idv,
event : doc.event
};
emit(null, newDoc);
}
}
Reduce
function(keys, values, rereduce) {
var result = [];
var closeMap = {};
for (var i=0; i<values.length; i++){
var doc = values[i];
if (doc.event=='CLOSE'){
closeMap[doc.problemNumber] = 1;
}
}
for (var i=0; i<values.length; i++){
var doc = values[i];
if (doc.event=='OPEN'){
if (closeMap[doc.problemNumber]){
doc.event = 'CLOSE';
}
result.push(doc);
}
}
return result;
}

Using underscore.js groupBy with Ember.js

Is it possible to use underscore's groupBy function with ember.js?
I have the following attempt which is obviously not working:
var activities = App.store.findMany(App.Activity, feed.mapProperty('id').uniq())
var grouped = _.groupBy(activities, function(activity){
return activity.get('dateLabel;')
});
I get the following error:
Object App.Activity has no method 'get'
The store is loaded with the correct data so findMany will not make a remote call.
The problem is that findMany returns a DS.ManyArray which is probably a lot different than what _.groupBy is looking for.
You could implement your own groupBy function tailored for ember-data DS-ManyArray objects and extend _ with it:
_.emberArrayGroupBy = function(emberArray, val) {
var result = {}, key, value, i, l = emberArray.get('length'),
iterator = _.isFunction(val) ? val : function(obj) { return obj.get(val); };
for (i = 0; i < l; i++) {
value = emberArray.objectAt(i);
key = iterator(value, i);
(result[key] || (result[key] = [])).push(value);
}
return result;
};
Now you can call
var grouped = _.emberArrayGroupBy(activities, function(activity) {
return activity.get('dateLabel');
});
or more simply
var grouped = _.emberArrayGroupBy(activities, 'dateLabel');
The function above is based on underscore's original groupBy() implementation, which looks very similar:
_.groupBy = function(obj, val) {
var result = {};
var iterator = _.isFunction(val) ? val : function(obj) { return obj[val]; };
each(obj, function(value, index) {
var key = iterator(value, index);
(result[key] || (result[key] = [])).push(value);
});
return result;
};
Try this code:
var activities = App.store.findMany(App.Activity, feed.mapProperty('id').uniq())
var grouped = _.groupBy(activities, function(activity){
return activity.get('dateLabel;')
}).bind(this);
I did not run this code to test how it works but the idea is to 'bind' outer scope into inner closure function scope.
Hope this helps to get you some ideas...

computed properties not getting updated

Problem: the computed property "tnop" does not get updated when the controller modifies the "pages" array as shown below:
relevant book model code:
sections: []
tnop: function()
{
var n = 0;
var sections = this.get('sections');
if (!sections)
return n;
for (var i=0; i < sections.length; i++) {
n += sections[i].pages.length;
}
return n;
}.property('sections.#each.pages.length')
relevant controller code:
var sections = EM.model.getPath('book.sections');
var aps = EM.model.getPath("ps");
$.each(sections, function(si, section) {
var bp = [];
if (json && json.ia) {
bp = section.get("pages");
var ns = aps[json.ssi];
var lp = ns.get("lp");
var rp = ns.get("rp");
if (rp) {
bp.insertAt(lp.number-1, rp);
bp[lp.number-1] = rp;
}
if (lp) {
bp.insertAt(lp.number-1, lp);
bp[lp.number-1] = lp;
}
}
});
As it stands, you can't observe deeply nested paths off #each directly. Instead, you have to use nested #each proxies, as such:
property('sections.#each.pages.#each.length')
Had the same problem solved using with following code:
filters: [],
content: [],
filterFun: function()
{
}.property('content.#each', 'filters.#each').cacheable(),