I'm the newbie in dart and flutter. I have try any code of javascript to solve it, but it's doesn't work:(.
I want get length of orders, but I don't want get the same value from the destination.id. Any idea for this case? Is possible to use library queries.dart or anything for solve this case?
This is my JSON data.
{
"data": {
"shipments": [
{
"id": "1608",
"orders": [
{
"destination": {
"id": "979"
}
},
{
"destination": {
"id": "979"
}
},
{
"destination": {
"id": "1074"
}
},
{
"destination": {
"id": "1074"
}
},
{
"destination": {
"id": "1022"
}
},
{
"destination": {
"id": "1022"
}
},
]
},
{
"id": "1599",
"orders": [
{
"destination": {
"id": "979"
}
},
{
"destination": {
"id": "979"
}
},
{
"destination": {
"id": "1660"
}
},
{
"destination": {
"id": "1660"
}
},
]
}
]
}
}
Try:
var data = ... your JSON data ...;
List orders = data["data"]["shipments"]["orders"];
var uniqueIds = orders.map((o) => o["destination"]["id"]).toSet();
var uniqueCount = uniqueIds.length;
This collects the unique IDs in a Set, which ensures that duplicates are ignored, and counts them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With