I have a list of data array from an API and I need to check the id with another hardcoded data array id.And I need to add two data elements which I'm getting from the api data array to the hardcoded array.
This is the hardcoded data array.
List<Data> data = [
Data(id: 1, title: 'title 1', comment: null, selected: null),
Data(id: 2, title: 'title 2', comment: null, selected: null),
Data(id: 3, title: 'title 3', val: null, comment: null, selected: null),
];
This is the data response from the API.
"data": [
{
"id": 1,
"value": "value 1",
"comment": "comment 1",
"userId": 1,
},
{
"id": 2,
"value": "value 2",
"comment": "comment 2",
"userId": 2,
},
{
"id": 3,
"value": "value 3",
"comment": "comment 3",
"userId": 3,
},
]
What I wanna do is the value I'm getting for comment and value from this response data list to add to the hardcoded array comment and selected.They're initially null.
Consider a dynamic list
List<dynamic> data = [
{ 'id': 1, 'title': 'title 1', 'comment': null, 'selected': null },
{ 'id': 2, 'title': 'title 2', 'comment': null, 'selected': null },
{ 'id': 3, 'title': 'title 3', 'val': null, 'comment': null, 'selected': null},
];
and the response from api is
List<dynamic> apiResp = [
{
"id": 1,
"value": "value 1",
"comment": "comment 1",
"userId": 1,
},
{
"id": 2,
"value": "value 2",
"comment": "comment 2",
"userId": 2,
},
{
"id": 3,
"value": "value 3",
"comment": "comment 3",
"userId": 3,
},
];
to update the comment and selected fields from api response you can do something like this
for (var each in data) {
var index = apiResp.indexWhere((element) => element!['id']!.toString() == each!['id']!.toString());
if (index > -1) {
data[index]!['comment'] = apiResp[index]!['comment'];
data[index]!['selected'] = apiResp[index]!['selected'];
}
}
if you want to make sure your data is updated just print it like this
// print to ensure the data is update
for (var each in data) {
print(each);
}
Note: I added .toString with id incase if you are not sure about data type and also if your response from api is not in list form you can convert it,
var data = json.decode(utf8.decode(response.bodyBytes));
return data.toList();
Related
Here I'm registering users by category from the app but can't register. But when I register a user from postman it can. What is the issue, kindly help me on this. Thanks.
Here is the scenario.
Here are id's for categories
{
"total": 6,
"per_page": 15,
"current_page": 1,
"last_page": 1,
"next_page_url": "",
"prev_page_url": "",
"from": 0,
"to": 6,
"success": true,
"en_message": "",
"ar_message": "",
"data": [
{
"id": 3,
"ar_name": "ستوديو",
"en_name": "Studio",
"photo": "http://newprotectco.com/hello/SD08/msf/Untitled-2-06.webp",
"parent": 0,
"parent_details": null
},
{
"id": 4,
"ar_name": "تطبيقات الموبايل والويب",
"en_name": "Web & App",
"photo": "http://newprotectco.com/hello/SD08/msf/Untitled-2-07.webp",
"parent": 0,
"parent_details": null
},
{
"id": 2,
"ar_name": "طباعة الصحيفه",
"en_name": "Printing Press",
"photo": "http://newprotectco.com/hello/SD08/msf/Untitled-2-05.webp",
"parent": 0,
"parent_details": null
},
{
"id": 5,
"ar_name": "الفعاليات",
"en_name": "Events",
"photo": "http://newprotectco.com/hello/SD08/msf/Untitled-2-08.webp",
"parent": 0,
"parent_details": null
},
{
"id": 1,
"ar_name": "التواصل الاجتماعي",
"en_name": "Social Media",
"photo": "http://newprotectco.com/hello/SD08/msf/Untitled-2-04.webp",
"parent": 0,
"parent_details": null
},
{
"id": 6,
"ar_name": "التصميم الداخلي",
"en_name": "Interior Design",
"photo": "http://newprotectco.com/hello/SD08/msf/Untitled-2-09.webp",
"parent": 0,
"parent_details": null
}
],
"status": 200
}
When I register a user by category from postman It can be.
I'm only adding field for category where I'm facing issues and the rest are working.
Category filed form postman
From the application when I try to register it doesn't show. Why?
Map<String, String> bdy = {
"name": companyName,
"username": email,
"password": password,
"phone": phone,
"trade_license": tradeLicence,
"category_id": categories,
};
Where categories are in drop down like this.
List<String> category = [
'Social Media',
'Printing Press',
'Studio',
'Web & App',
'Events',
'Interior Design',
];
String? selectedCategory;
void _agencyRegistration() {
else if ((selectedCategory ?? "").isEmpty) {
Utils.flushBarErrorMessage(LanguageStringKeys.instance.TypeYourField.tr, context);
}
Provider.of<AgencyRegistrationService>(context, listen: false).doSignUp(
context,
companyName,
email,
password,
phone,
(selectedCategory?? ""),
image,
);
DropdownButtonFormField(
isExpanded: true,
decoration: textFormFieldsDecoration(
LanguageStringKeys.instance.Category.tr,
AppColors.fIconsAndTextColor,
AppColors.fIconsAndTextColor,
),
items: category
.map((item) => DropdownMenuItem(
value: item,
child: Center(child: SmallText(text: item, color: Colors.black)),
))
.toList(),
value: selectedCategory,
onChanged: (item) => setState(() => selectedCategory = item as String?)),
}
I have this response from an API
{
"error": "",
"success": {
"data": [
{
"id": "00005432",
"channel": "B22",
"case_status_id": "1",
"case_type_id": "0",
"provider_group_id": "1",
"user_id": "78",
"contact_id": null,
"client_id": "23982",
"subject": "parallala",
"description": "",
"latest_development": "",
"priority": "medium",
"caseArrivalDate": "2022-08-29",
"arrivalDate": "2022-08-29",
"dueDate": null,
"closedOn": null,
"statusComments": "dsdde 663171471 (ndnendn ))////#-#-&#+#+#+#+#(",
"category": "edefrfr",
"caseValue": "0.00",
"internalReference": null,
"externalizeLawyers": "no",
"createdOn": "2022-08-29 10:59:46",
"createdBy": "14",
"modifiedOn": "2022-09-27 18:35:43",
"requestedByName": null,
"legal_case_stage_id": null,
"caseClientPosition": null,
"clientForeignName": null
}
],
"totalRows": "1122",
"dbDriver": "MYSQL"
}
}
I think its a Map(keys, values) and inside the key(success) we have another Map
How I can get access to a specific element?
I will assume that this value is assigned to a map type variable, I will name it response
so to access the id :
response["success"]["data"][0]["id"]
first you should know how to access to Map
here Ex show you how can get access to a specific element
myset is Custom Map you provided it
((myset['success'] as Map<String, dynamic>)['data'] as List)
.where((e) => e["id"] == "00005432");
you will get list of Map And Filter List By specific Id Or Any attributes you need
after You understand it you can parse Map To Object And Access easyly To All attributes
hope this help you to get Started
JSON and serialization
for ex. that is my data
const String _SexData = """[
{
"sex": "Seçiniz / Choice",
"x": 1000
},
{
"sex": "Male",
"x": 1001
},
{
"sex": "Female",
"x": 1002
}
]""";
and I calling it;
DropdownButton<int>(
iconEnabledColor: Colors.blue,
dropdownColor: const Color.fromARGB(255, 28, 185, 242),
style: const TextStyle(color: Colors.black),
value: sex?.x ?? 1000,
items: model1.sexs.map((a) {
return DropdownMenuItem(
child: Text(a.sex,
style: const TextStyle(color: Colors.white)),
value: a.x,
onTap: () {
setState(() {
sex = a;
});
},
);
}).toList(),
onChanged: (s) {},
),
I have difficulties on how to group sets of flutter widgets based on data returned from server.
The sample data is as below:
{
id: 1,
group: 1,
name: A,
},
{
id: 2,
group: 1,
name: B,
},
{
id: 3,
group: 2,
name: C,
},
{
id: 3,
group: null,
name: D,
}
Lets say those group that not null will group into a set of Dropdownlist while others are checkboxed, I have been trying so many hours by still cant and I am new to Flutter.
Is that a List? You can group by filtering with where, i.e., data.where((x) => x["group"] != null).
Full Dart code, see dartpad.dev:
void main() {
var data = [
{
"id": 1,
"group": 1,
"name": "A",
},
{
"id": 2,
"group": 1,
"name": "B",
},
{
"id": 3,
"group": 2,
"name": "C",
},
{
"id": 3,
"group": null,
"name": "D",
}
];
print(data.where((x) => x["group"] != null));
}
I want to bind the dynamic Dropdowns for each row in SAPUI5 table. There should be the row-specific data for each row-Dropdown. Here is the sample json having three rows. But I'm unable to bind the row-specific data for each dropdown. Thanks in Advance!
Here, I have simple table.
// Table goes here
var demoTbl = new sap.ui.table.Table({
visibleRowCount: 10,
width : "100%",
selectionMode: sap.ui.table.SelectionMode.Multi,
});
var systemColumn = new sap.ui.table.Column({
width:"12%",
label: new sap.ui.commons.Label({text: "System", design:sap.ui.commons.LabelDesign.Bold}),
template: new sap.ui.commons.TextField({editable:false}).bindProperty("value", "name"),
sortProperty: "name",
filterProperty: "name",
sorted : false,
filtered : false
});
demoTbl.addColumn(systemColumn);
bind list data for 1st row-Dropdown List here-
var inputListBox = new sap.ui.commons.ListBox();
inputListBox.bindAggregation("items","/listData/dataList/0/dropList",function(oId,oContext){
console.log(oContext);
return new sap.ui.core.ListItem({
key: oContext.getProperty("id"),
text: oContext.getProperty("name")
});
});
var connectorIpColumn = new sap.ui.table.Column({
width:"12%",
label: new sap.ui.commons.Label({text: "Dropdown Data", design:sap.ui.commons.LabelDesign.Bold}),
template: new sap.ui.commons.DropdownBox({
"association:listBox" : inputListBox
})
});
demoTbl.addColumn(connectorIpColumn);
And, here is the Data -
var oData={
"dataList": [{
"id": 111,
"name": "Row1 Data",
"dropList": [
{"id": 1, "name": "Row1 dropDown Item1"},
{"id": 2, "name": "Row1 dropDown Item2"},
{"id": 3, "name": "Row1 dropDown Item3"},
{"id": 4, "name": "Row1 dropDown Item4"}
]
},
{
"id": 222,
"name": "Row2 Data",
"dropList": [
{"id": 5, "name": "Row2 dropDown Item1"},
{"id": 6, "name": "Row2 dropDown Item2"},
{"id": 7, "name": "Row2 dropDown Item3"}
]
},
{
"id": 333,
"name": "Row3 Data",
"dropList": [
{"id": 8, "name": "Row3 dropDown Item1"},
{"id": 9, "name": "Row3 dropDown Item2"},
{"id": 10, "name": "Row3 dropDown Item3"}
]
}
]};
Bind data here-
var mappingModel = new sap.ui.model.json.JSONModel({listData:oData});
sap.ui.getCore().setModel(mappingModel, "mappingModel");
demoTbl.setModel(mappingModel);
demoTbl.bindRows("/listData/dataList");
mappingModel.refresh(true);
var addSystemPage = new sap.m.Page( {
content:[demoTbl]
});
You'll have to provide the aggregation binding path for ListBox template as 'dropList'.
inputListBox.bindAggregation("items","dropList",function(oId,oContext){
return new sap.ui.core.ListItem({
key: oContext.getProperty("id"),
text: oContext.getProperty("name")
});
});
Here is the simple table having three rows, and each row contains a DropdownBox with listItems. But the DropdownBox in the second row is empty. I want to hide the blank DropdownBox. Can we hide the empty DropdownBox from that row, so that it will look just a simple blank cell. Thanks in Advance!
Here, I have simple table.
var demoTbl = new sap.ui.table.Table({
visibleRowCount: 10,
width : "100%",
selectionMode: sap.ui.table.SelectionMode.Multi,
});
var systemColumn = new sap.ui.table.Column({
width:"12%",
label: new sap.ui.commons.Label({text: "Column Data", design:sap.ui.commons.LabelDesign.Bold}),
template: new sap.ui.commons.TextField({editable:false}).bindProperty("value", "name"),
sortProperty: "name",
filterProperty: "name",
sorted : false,
filtered : false
});
demoTbl.addColumn(systemColumn);
var inputListBox = new sap.ui.commons.ListBox();
inputListBox.bindAggregation("items","dropList",function(oId,oContext){
return new sap.ui.core.ListItem({
key: oContext.getProperty("id"),
text: oContext.getProperty("name")
});
});
var connectorIpColumn = new sap.ui.table.Column({
width:"12%",
label: new sap.ui.commons.Label({text: "Dropdown Data", design:sap.ui.commons.LabelDesign.Bold}),
template: new sap.ui.commons.DropdownBox({
"association:listBox" : inputListBox
})
});
demoTbl.addColumn(connectorIpColumn);
And, here is the Data -
var oData={
"dataList": [{
"id": 111,
"name": "Row1 Data",
"dropList": [
{"id": 1, "name": "Row1 dropDown Item1"},
{"id": 2, "name": "Row1 dropDown Item2"},
{"id": 3, "name": "Row1 dropDown Item3"},
{"id": 4, "name": "Row1 dropDown Item4"}
]
},
{
"id": 222,
"name": "Row2 Data",
"dropList": []
},
{
"id": 333,
"name": "Row3 Data",
"dropList": [
{"id": 8, "name": "Row3 dropDown Item1"},
{"id": 9, "name": "Row3 dropDown Item2"},
{"id": 10, "name": "Row3 dropDown Item3"}
]
}
]};
var mappingModel = new sap.ui.model.json.JSONModel({listData:oData});
sap.ui.getCore().setModel(mappingModel, "mappingModel");
demoTbl.setModel(mappingModel);
demoTbl.bindRows("/listData/dataList");
mappingModel.refresh(true);
var addSystemPage = new sap.m.Page("addSystemPageId", {
content:[demoTbl]
});
There are many ways reading the cells of the table and determining the dropdown values and explicitly setting the visibility. I would propose the best way is to
var oData={
"dataList": [{
"id": 111,
"name": "Row1 Data",
"dropVis" : true,
"dropList": [
{"id": 1, "name": "Row1 dropDown Item1"},
{"id": 2, "name": "Row1 dropDown Item2"},
{"id": 3, "name": "Row1 dropDown Item3"},
{"id": 4, "name": "Row1 dropDown Item4"}
]
},
{
"id": 222,
"name": "Row2 Data",
"dropVis" : false,
"dropList": []
},
{
"id": 333,
"name": "Row3 Data",
"dropVis" : true,
"dropList": [
{"id": 8, "name": "Row3 dropDown Item1"},
{"id": 9, "name": "Row3 dropDown Item2"},
{"id": 10, "name": "Row3 dropDown Item3"}
]
}
]};
You can see the json object has been modified to get one attribute dropVis this can manually filled you you based on dropList and finally bind this attribute to the call template
var connectorIpColumn = new sap.ui.table.Column({
width:"12%",
label: new sap.ui.commons.Label({text: "Dropdown Data", design:sap.ui.commons.LabelDesign.Bold}),
template: new sap.ui.commons.DropdownBox({
visible : "{dropVis}",
"association:listBox" : inputListBox
})
});
The visibility is bound directly and it should work.
You can make use of formatter to toggle visibility based on length of dropList Array.
template: new sap.ui.commons.DropdownBox({
visible: {
path: 'dropList',
formatter: function(aList) {
return aList ? !!aList.length : false;
}
}
});