I have data like:
[
{name: Imamat, short_name: Lev, chapters: 27, name_en: Leviticus, type: old, order: 3},
{name: Kejadian, short_name: Gen, chapters: 50, name_en: Genesis, type: old, order: 1},
//....
]
I need to return this data sorted by order value like 1,2,3,4,.... Here is my function that returns results in up:
readDataBase() async {
String data = await DefaultAssetBundle.of(context).loadString("assets/db/tb.json");
final jsonResult = jsonDecode(data);
return jsonResult['book'];
}
This should do the trick efficiently;
List items = [
{"name": "Imamat", "short_name": "Lev", 'chapters': 27, "name_en": "Leviticus", "type": "old", "order": 3},
{"name": "Kejadian", "short_name": "Gen", "chapters": 50, "name_en": "Genesis", "type": "old", "order": 1},
];
getOrderNo (e)=>e["order"];
items.sort((a, b) => getOrderNo(a).compareTo(getOrderNo(b)));
print(items);
Related
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();
I am new in Dart and I have been trying to access the value of "farm_size" key in the following piece of code but unsuccessful. I can access up to "farms" like this
print(myData1[0]["farms"]);
But not any further. Any help will be appreciated. Thanks.
void main() {
var myData1 = [
{
"id": 1,
"first_name": "Jordan",
"sur_name": "Zamunda",
"member_n0": "AA1",
"gender": "male",
"phone": "123456789",
"email": "jordan#example.com",
"farms": [
{"id": 1, "farm_name": "SOUTH_FARM", "farm_size": "160 acres"},
{"id": 2, "farm_name": "NORTH_FARM", "farm_size": "190 acres"}
]
}
];
print(myData1[0]["farms"]);
}
inside farms, there is a list of more maps, to access those maps, you need to specify the index of the map you, for example we want to get the first map, we type:
print(myData1[0]["farms"][0]);
//this will print => {"id": 1, "farm_name": "SOUTH_FARM", "farm_size": "160 acres"}
//to get the name of this farm, you use the key 'farm_name' now:
print(myData1[0]["farms"][0]['farm_name']);
//prints=> SOUTH_FARM
If you're running it in dartpad, this will work:
void main() {
var myData1 = [
{
"id": 1,
"first_name": "Jordan",
"sur_name": "Zamunda",
"member_n0": "AA1",
"gender": "male",
"phone": "123456789",
"email": "jordan#example.com",
"farms": [
{"id": 1, "farm_name": "SOUTH_FARM", "farm_size": "160 acres"},
{"id": 2, "farm_name": "NORTH_FARM", "farm_size": "190 acres"}
]
}
];
List<Map<String,dynamic>> _list = myData1[0]["farms"]as List<Map<String,dynamic>> ;
print(_list[0]['farm_name']);
}
try this
List m = myData1[0]['farms'] as List;
print(m[0]['id']);
I have two lists and I want to make a new list by comparing the two lists and the new list should only contain those elements that contain in each of them.
List1=[[id: 1, label: 'cocoa'],[id: 2, label: 'apple'],[id: 3, label: 'cherry'],[id: 4, label: 'banana'],[id: 5, label: 'melon'],[id: 6, label: 'orange'],[id: 7, label: 'pineapple'],[id: 8], label: 'strawberry']
List2=[2,5,7,8]
expectedList = [[id: 2, label: 'apple'],[id: 5, label: 'melon'],[id: 7, label: 'pineapple'],[id: 8], label: 'strawberry']]
And the real code down below:
"options": [
{
"id": 58,
"label": "cocoa",
"swatch_value": null,
"products": [
118
]
},
{
"id": 59,
"label": "dark chocolate",
"swatch_value": null,
"products": [
120,
127
]
},
{
"id": 60,
"label": "apple",
"swatch_value": null,
"products": [
121,
128
]
},
{
"id": 61,
"label": "milk",
"swatch_value": null,
"products": [
122
]
},
{
"id": 62,
"label": "coconut",
"swatch_value": null,
"products": [
130
]
},
{
"id": 65,
"label": "cherry",
"swatch_value": null,
"products": [
126
]
}
]
So the first list would contain the json above, and the second list contains the numbers below that equals to some of the ID.
List<int> secondList = [58, 59, 60, 61];
Now when I want to generate a new list by comparing the secondList with the ID's of the first list, the third list is empty.
List thirdList = firstList.options.where((element) => secondList.contains(element.id)).toList();
var list1 = [1,2,3,4,5,6,7,8];
var list2 = [2,5,7,8];
var expectedList = list1.toSet().intersection(list2.toSet()).toList();
print(expectedList.toString());
More information here.
Respect to your info:
var dataList = data['options'];
var firstList = List<int>.generate(dataList.length, (i) => dataList[i]['id']);
var secondList = [58, 59, 60, 61];
var thirdList = firstList.toSet().intersection(secondList.toSet()).toList();
var filtered = dataList.where((e) => thirdList.contains(e['id'])).toList();
print(filtered.toString());
where:
const data = {
"options": [
...
]
};
The result is:
[{id: 58, label: cocoa, swatch_value: null, products: [118]}, {id: 59, label: dark chocolate, swatch_value: null, products: [120, 127]}, {id: 60, label: apple, swatch_value: null, products: [121, 128]}, {id: 61, label: milk, swatch_value: null, products: [122]}]
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 am new to SAPUI5, I am trying to get multiple dimensions in Bar chart using VizFrame. I need chart to be displayed like the below image:
Please check my code.
I think you are looking for Bar graph to show the Amount vs Days.
Here is the working link.
I have updated _setupChart methods as below.
_setupChart: function() {
var oVizFrame = this.getView().byId("idVizFrame");
oVizFrame.setModel(new JSONModel('./data.json'));
var oDataset = new sap.viz.ui5.data.FlattenedDataset({
dimensions: [{
name: "Days",
value: "{Days}"
}],
measures: [{
name: "Amount",
value: "{Amount}"
}],
data: {
path: "/dueDays"
}
});
oVizFrame.setDataset(oDataset);
oVizFrame.setVizType('bar');
var feedValueAxis = new sap.viz.ui5.controls.common.feeds.FeedItem({
"uid": "valueAxis",
"type": "Measure",
"values": ["Amount"]
});
var feedCategoryAxis = new sap.viz.ui5.controls.common.feeds.FeedItem({
"uid": "categoryAxis",
"type": "Dimension",
"values": ["Days"]
});
oVizFrame.addFeed(feedValueAxis);
oVizFrame.addFeed(feedCategoryAxis);
}
And also i have updated the data json as below
{
"dueDays": [{
"Days": "Current",
"Amount": "44334.00"
}, {
"Days": "1 to 30",
"Amount": "53454.00"
}, {
"Days": "31 to 60",
"Amount": "34443.65"
}, {
"Days": "61 to 90",
"Amount": "65554.43"
}, {
"Days": "91 to 120",
"Amount": "43524.00"
},{
"Days": "121 to 150",
"Amount": "54554.00"
}, {
"Days": "151 to 180",
"Amount": "43324.00"
}, {
"Days": "Above 180",
"Amount": "54355"
}]
}
Thanks for your work & time !! .. I have made it using setVizProperties > plotArea > dataPointStyle > rules .. This is the updated link.