What's the best way to sort MAP<String, Object> by value in Dart programming? - list

I'm new to dart programming, I came from a Javascript environment
everything is fresh to me and I really have no Idea how this sorting works
in dart.
Here's an example from my API
List example = [
{"id": "1", "age": "20"},
{"id": "2", "age": "21"},
{"id": "3", "age": "22"},
]
Before I will assign "example" to other variables eg.
var example2 = example;
I want to sort it by "age", I found libs and other "LONG" solutions out there but feels like there's another way.. Thanks in advance!

There is sort function for list. Try reading the documentation a bit:
void main() {
List example = [
{"id": "1", "age": "20"},
{"id": "2", "age": "19"},
{"id": "3", "age": "22"},
{"id": "4", "age": "9"},
];
print(example);
example.sort((x,y) => x["age"].compareTo(y["age"]));
print(example);
}
EDIT: Your definition should contain integers logically:
void main() {
List example = [
{"id": 1, "age": 20},
{"id": 2, "age": 19},
{"id": 3, "age": 22},
{"id": 4, "age": 9},
];
print(example);
example.sort((x,y) => x["age"].compareTo(y["age"]));
print(example);
}

Related

Sorting a list into smaller lists according to a characteristic in dart

Sorry for my poor explanation, I just started learning dart.
With a mock service and a json file I created a set amount of items
Example:
{
"items": [
{
"id": "01",
"type": "a"
},
{
"id": "02",
"type": "b"
},
{
"id": "03",
"type": "c"
}
]
}
when creating the list on the service it creates a single list like this:
if (json['items'] != null) {
final itemList = <ItemList>[];
json['items'].forEach((v) {
itemlistList.add(ItemList.fromJson(v));
});
return ItemList;
} else {
return [];
}
is there a way to, form the create list step to already separate them into 3 different lists for the type a, b, and c items? and if now, where and how would I divide this itemlist into 3 based on the type characteristic of each item?
Using groupBy, as suggested in this extremely similar question: Flutter/Dart how to groupBy list of maps?
import "package:collection/collection.dart";
main(List<String> args) {
var data = [
{"id": "01", "type": "a"},
{"id": "02", "type": "b"},
{"id": "03", "type": "c"},
{"id": "04", "type": "a"},
{"id": "05", "type": "a"},
{"id": "06", "type": "b"},
];
var newMap = groupBy(data, (Map obj) => obj["type"]);
print(newMap);
}

Can not check the JSON value from the response body

I just started testing the api using the postman. I have come across one problem with the json path..
in json file I have code which looks like this
{
"users": [
{
"firstName": "UserOne",
"lastName": "One",
"subjectId": 1,
"id": 1
},
{
"firstName": "UserTwo",
"lastName": "Two",
"subjectId": 2,
"id": 2
},
{
"firstName": "UserThree",
"lastName": "Three",
"subjectId": 3,
"id": 3
}
],
"subjects": [
{
"id": 1,
"name": "SubOne"
},
{
"id": 2,
"name": "SubTwo"
},
{
"id": 3,
"name": "SubThree"
}
]
}
I start the json server with json-server --watch db.json
After that I send GET request using postman
Request URL http://localhost:3000/users
this is the body I get
[
{
"firstName": "UserOne",
"lastName": "One",
"subjectId": 1,
"id": 1
},
{
"firstName": "UserTwo",
"lastName": "Two",
"subjectId": 2,
"id": 2
},
{
"firstName": "UserThree",
"lastName": "Three",
"subjectId": 3,
"id": 3
}
]
I was trying to verify the firstName from the id 1 using the snippet code below
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.users[0].firstName).to.eql('UserOne');
});
But the output was
FAIL Your test name | TypeError: Cannot read property '0' of undefined
I need help here because the json path users[0].firstName should give me the first index..
There is no key users in your response, so when you try to access the non-existed key, you will get error Cannot read property 'xxx' of undefined. To fix that, you just need:
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData[0].firstName).to.eql('UserOne');
});

Data is not validated in Django Rest Framework

I am trying to pass the data to a serializer like the following:
myDict = {
"invoice_date": "2021-02-24T11:44:13+05:30",
"invoice_number": "12",
"vendor": "4",
"amount": "12",
"gst": "12",
"total_amount": "14",
"transaction_type": "Allot",
"status": "Hold",
"transactions": [
{
"t_no": 47,
"f_mile": "45",
"long_haul": "45",
"l_mile": "45",
"labour": "45",
"others": "54",
"a_t_no": 47,
},
{
"t_no": 102,
"f_mile": "12",
"long_haul": "12",
"l_mile": "21",
"labour": "21",
"others": "21",
"a_t_no": 102,
},
],
"owner": 2,
}
But when I check the validated data in the serialzer it shows it without the transactions data:
{'invoice_date': datetime.datetime(2021, 2, 24, 6, 14, 13, tzinfo=<UTC>), 'invoice_number': '12', 'amount': 12, 'gst': 12, 'total_amount': 14, 'status': 'Hold', 'transaction_type': 'Allot', 'vendor': <Vendor: Vendor object (4)>, 'owner': <User: yantraksh>}
so I tried to check the initial data that is being passed to the serializer :
<QueryDict: {
"invoice_date": ["2021-02-24T11:44:13+05:30"],
"invoice_number": ["12"],
"vendor": ["4"],
"amount": ["12"],
"gst": ["12"],
"total_amount": ["14"],
"transaction_type": ["Allot"],
"status": ["Hold"],
"transactions": [
'[{"t_no":47,"f_mile":"45","long_haul":"45","l_mile":"45","labour":"45","others":"54","a_t_no":47},{"t_no":102,"f_mile":"12","long_haul":"12","l_mile":"21","labour":"21","others": "21","a_t_no":102}]'
],
"owner": [2],
}>
It shows that the transaction data is being passed as a string, what should I change it to in order to get it as validated data ?
Based on the "evidence" in that QueryDict (namely strings wrapped in lists) it sounds like you're not submitting the data as JSON.
You can't use regular HTML form data to submit structured data, you'll need to post JSON data.

How to add a list in a list to another list

I have a list called test3 and I want to add this list to another list test0. The problem is that I only want the "noequipment" "sublist" to be added to the test0.
I tried it with the following code:
var test0 = test0 + test3["noequipment"];
This is list test3:
var test3 = [
{
"noequipment": [
{"name": "test0.1", "time": 10},
{"name": "test0.2", "time": 10},
{"name": "test0.3", "time": 10},
{"name": "test0.4", "time": 10},
]
},
{
"equipment1": [
{"name": "test1.1", "time": 10},
{"name": "test1.2", "time": 10},
{"name": "test1.3", "time": 10},
{"name": "test1.4", "time": 10},
]
},
];
Maybe you are looking for this answer
for (var i in test3[0]['noequipment']) {
test0.add(i);
print("===========>>${test0}");
}
Use code below assuming test0 is already an array
test0.addAll(test3[0]['noequipment'])
Please test this code.
test0.addAll(test3[0]["noequipment"]);
Make sure parent list is defined as var test0 = [] ;
Here I test this code https://pastebin.pl/view/ff02c9cc

Does ember.js support segment-literal notation?

Ember.js doesnt seem to support json objects with dots in the objects attribute names (e.g.):
{ "ns1.id" : 14, "ns2.name" : "mike" };
Is this an omission or am i overlooking something? We have services that we are required to consume that support namespaces in this way.
Handlebars seems to support the above using segment-literal notation (e.g. {{[nd.id]}}). Is that also true of Ember? Is the documentation I have missed?
Thanks!
Yes you can do this. It is compound documents which is used to save HTTP requests, it may be convenient to send related documents along with the requested documents.
In this case, a bit of extra metadata for each relationship can link together the documents. Following example shows use of compound documents:
{
"rels": {
"posts.author": {
"url": "http://example.com/people/{post.author}",
"type": "people"
},
"posts.comments": {
"url": "http://example.com/comments/{post.comments}",
"type": "comments"
}
}
"posts": [{
"id": 1,
"title": "Rails is Omakase",
"rels": {
"author": 9,
"comments": [ 1, 2, 3 ]
}, {
"id": 2,
"title": "The Parley Letter",
"rels": {
"author": 9,
"comments": [ 4, 5 ]
}, {
"id": 1,
"title": "Dependency Injection is Not a Virtue",
"rels": {
"author": 9,
"comments": [ 6 ]
}
}],
"people": [{
"id": 9,
"name": "#d2h"
}],
"comments": [{
"id": 1,
"body": "Mmmmmakase"
}, {
"id": 2,
"body": "I prefer unagi"
}, {
"id": 3,
"body": "What's Omakase?"
}, {
"id": 4,
"body": "Parley is a discussion, especially one between enemies"
}, {
"id": 5,
"body": "The parsley letter"
}, {
"id": 6,
"body": "Dependency Injection is Not a Vice"
}]
}