dart flutter max value from list of objects - flutter

Looking for some help how to select oldest age from users in flutter object list...
users = [
{ id: 123, name: 'Bob', age: 25},
{ id: 345, name: 'Joe', age: 44},
...
];

First make sure your list has the correct type and format:
List<Map<String, dynamic>> users = [
{"id": 123, "name": "Bob", "age": 25},
{"id": 345, "name": "Joe", "age": 44},
{"id": 35, "name": "Joxe", "age": 40},
];
Then you can do this:
if (users != null && users.isNotEmpty) {
users.sort((a, b) => a['age'].compareTo(b['age']));
print(users.last['age']);
}
Another way would be:
if (users != null && users.isNotEmpty) {
dynamic max = users.first;
users.forEach((e) {
if (e['age'] > max['age']) max = e;
});
print(max['age']);
}
Another one:
if (users != null && users.isNotEmpty) {
print(users.fold<int>(0, (max, e) => e['age'] > max ? e['age'] : max));
}
And this one requires import 'dart:math':
if (users != null && users.isNotEmpty) {
print(users.map<int>((e) => e['age']).reduce(max));
}

I'd use the list reduce function, docs here.
var oldestUser = users.reduce((currentUser, nextUser) => currentUser['age'] > nextUser['age'] ? currentUser : nextUser)

void main() {
var users = [
{"id": 123, "name": 'Bob', "age": 25},
{"id": 345, "name": 'Joe', "age": 44},
{"id": 35, "name": 'Joxe', "age": 40},
];
users.sort(ageCompare);
print(users.first);
}
int ageCompare(u1, u2) => u2['age'] - u1['age'];
try it on https://dartpad.dartlang.org
or just one-liner
users.sort((Map u1, Map u2) => u2['age'] - u1['age']);
print(users.first);

Related

Flutter how to link arrays by ids

I have data like this
var sendlocal = [
{
"firstName": "tree",
"lastName": "tree",
"relativeEmail": "tree#gmail.com",
"relativeType": 0,
"subid": 1,
"subRelatives": [
{
"firstName": "julia2",
"lastName": "Michle",
"relativeEmail": "test#hotmail.com3",
"relativeType": 2,
"subid": 2,
"subRelatives": [
{
"firstName": "john",
"lastName": "bravo",
"relativeEmail": "johny#gmail.com",
"relativeType": 1,
"subRelatives": [],
"subid": 3,
},
{
"firstName": "simith",
"lastName": "bravo",
"relativeEmail": "johny#gmail.com",
"relativeType": 1,
"subRelatives": [],
"subid": 4,
},
],
},
{
"firstName": "julia3",
"lastName": "Michle",
"relativeEmail": "test3#hotmail.com",
"relativeType": 2,
"subRelatives": [],
"subid": 5,
},
],
},
];
And as per below answer(Thanks) I create a function like this
getIndexFromNestedList(List<dynamic> mapValue) {
if (mapValue != null) {
for (var relation in mapValue) {
print(relation['subid']);
print(relation['relativeEmail']);
if (relation['subRelatives'] != null) {
for (var subRelation in relation['subRelatives']) {
print({relation['subid'], subRelation['subid']});
// graph.addEdge(relation['subid'], subRelation['subid']); //like this
Future.delayed(Duration(milliseconds: 1), () {
getIndexFromNestedList(relation['subRelatives']);
});
}
}
}
}
}
and pass data like this
var check = getIndexFromNestedList(relatives);
And I getting responses like this
flutter: {1, 2}
flutter: {1, 3}
4flutter: {2, null}
What Expected is {2,3} {2,4} and so on if sub relative also have sub relative. But it's showing null don't know why when it's going to sub relatives.
You can use a nested for loop to get those values like the following and resend the loop to the method to get the nested values
getIndexFromNestedList(List<dynamic> mapValue) {
for (var relation in mapValue) {
if (relation['sub'] != null) {
for (var subRelation in relation['sub']) {
print({relation['id'], subRelation['id']});//you can add it directly to the map here.
graph.addEdge(relation['id'], subRelation['id']);//like this
Future.delayed(Duration(milliseconds:1), (){//future is added to finish the first loop then to enter into the inner loops.
getIndexFromNestedList(relation['sub']);
});
}
}
}
}
//OUTPUT
{1, 2}
{1, 6}
{2, 3}
{2, 5}
{2, 3}
{2, 5}
{3, 4}
{3, 4}
{3, 4}
{3, 4}
you can use this method to set the values into the graph
getIndexFromNestedList(reList);
EDIT
for the current structure you can check if the first value is not null before you access its children like the following
getIndexFromNestedList(List<dynamic> mapValue) {
if (mapValue != null) {
for (var relation in mapValue) {
print(relation['subid']);
print(relation['relativeEmail']);
if (relation['subRelatives'] != null) {
for (var subRelation in relation['subRelatives']) {
print({relation['subid'], subRelation['subid']});
// graph.addEdge(relation['subid'], subRelation['subid']); //like this
Future.delayed(Duration(milliseconds: 1), () {
getIndexFromNestedList(relation['subRelatives']);
});
}
}
}
}
}
Edit on 7AUG
In the question again you missed to check if its a null value and if length is greater than 0. also you are passing relatives to the method.. The list name wassendlocal
Here is the updated method with length check too
getIndexFromNestedList(List mapValue) {
if (mapValue != null) {
for (int i = 0; i < mapValue.length; i++) {
for (int j = 0; j < mapValue[i]['subRelatives'].length; j++) {
Map subList = mapValue[i]['subRelatives'][j];
print({mapValue[i]['subid'], subList['subid']});
if (subList['subRelatives'].length > 0) {
Future.delayed(Duration(milliseconds: 10), () {
getIndexFromNestedList(mapValue[i]['subRelatives']);
});
}
}
}
}
}
//Output
flutter: {1, 2}
flutter: {1, 5}
flutter: {2, 3}
flutter: {2, 4}
Tested this multiple times and removed all duplicates too.
I got your answer as per your requirement, maybe this helpful for you
void main(){
var check =getIndexFromList(sendlocal);
}
getIndexFromList(List sendlocal) {
if (sendlocal != null) {
for (var relation in sendlocal) {
print('subid:- ${relation['subid']}');
print('relativeEmail:- ${relation['relativeEmail']}');
if (relation['subRelatives'] != null && relation['subRelatives'].length > 0) {
for (var subRelation in relation['subRelatives']) {
//its your required response
print({relation['subid'], subRelation['subid']});
Future.delayed(Duration(milliseconds: 2), () {
getIndexFromList(relation['subRelatives']);
});
}
}
}
}
}
var sendlocal = [
{
"firstName": "tree",
"lastName": "tree",
"relativeEmail": "tree#gmail.com",
"relativeType": 0,
"subid": 1,
"subRelatives": [
{
"firstName": "julia2",
"lastName": "Michle",
"relativeEmail": "test#hotmail.com3",
"relativeType": 2,
"subid": 2,
"subRelatives": [
{
"firstName": "john",
"lastName": "bravo",
"relativeEmail": "johny#gmail.com",
"relativeType": 1,
"subRelatives": [],
"subid": 3,
},
{
"firstName": "simith",
"lastName": "bravo",
"relativeEmail": "johny#gmail.com",
"relativeType": 1,
"subRelatives": [],
"subid": 4,
},
],
},
{
"firstName": "julia3",
"lastName": "Michle",
"relativeEmail": "test3#hotmail.com",
"relativeType": 2,
"subRelatives": [],
"subid": 5,
},
],
},
];
The response is something like that,
subid:- 1
relativeEmail:- tree#gmail.com
{1, 2}
{1, 5}
subid:- 2
relativeEmail:- test#hotmail.com3
{2, 3}
{2, 4}
subid:- 5
relativeEmail:- test3#hotmail.com
subid:- 2
relativeEmail:- test#hotmail.com3
{2, 3}
{2, 4}
subid:- 5
.............
.............

Flutter : How change modify unmodifiable map

I have list like this in provider:
List orders=[]:
void getOrders(){
orders = [
{"id":1,
"order":[
{"id":1,"name":"mike"},
{"id":2,"name":"john"},
{"id":3,"name":"smith"}
]
},
{"id":1,
"order":[
{"id":1,"name":"roz"},
{"id":2,"name":"sam"},
{"id":3,"name":"ruby"}
]
},
];
notifyListeners();
}
in provider when I use this methos to chane indexed order with another:
void changeOrder(orderIndex,item){
orders[orderIndex].update("order",(val)=>item);
notifyListeners();
}
I get this error type '(dynamic) => dynamic' is not a subtype of type '(Object) => Object' of 'update'
and when I use this :
void changeOrder(orderIndex,item){
orders[orderIndex]["order"]=item;
notifyListeners();
}
I get this error Unsupported operation: Cannot modify unmodifiable map
Add More Details
the item in changeOrder method comes from screen contain orders :
var item = List.from(orders[index]);
orders type is List<Map<String, dynamic>>. While reading the item, it will be a map instead of list.
Map item = Map.from(orders[index]);
And you can use both way you;ve tried.
List<Map<String, dynamic>> orders = [];
void getOrders() {
orders = [
{
"id": 1,
"order": [
{"id": 1, "name": "mike"},
{"id": 2, "name": "john"},
{"id": 3, "name": "smith"}
]
},
{
"id": 1,
"order": [
{"id": 1, "name": "roz"},
{"id": 2, "name": "sam"},
{"id": 3, "name": "ruby"}
]
},
];
}
void changeOrder(orderIndex, item) {
orders[orderIndex]["order"] = item;
// orders[orderIndex].update("order", (val) => item);
}
void main(List<String> args) {
getOrders();
print(orders);
Map item = Map.from(orders[1]);
changeOrder(1, item);
print(orders);
}

What is the cleanest way to get the average of each item list in Dart?

I have a List<Expenses> in dart. It looks like this:
[
{itemId: 1,
quantity: 10.0
},
{itemId: 2,
quantity: 14.0
},
{itemId: 2,
quantity: 25.0
},
{itemId: 3,
quantity: 3.0
}
...
];
I would like to get the average of each itemId to obtain a List like this:
[
{itemId: 1,
quantity: 10.0
},
{itemId: 2,
quantity: 19.5
},
{itemId: 3,
quantity: 3.0
}
...
];
What is the simplest way to do that?
I wouldn't say it is the simplest but it works:
void main() {
List data = [
{"itemId": 1, "quantity": 10.0},
{"itemId": 2, "quantity": 14.0},
{"itemId": 2, "quantity": 25.0},
{"itemId": 3, "quantity": 3.0}
];
List formattedList(List list) {
var obj = {};
for (var i = 0; i < list.length; i++) {
if (obj[list[i]["itemId"]] == null) {
obj[list[i]["itemId"]] = {"count": 1, "quantity": list[i]["quantity"]};
} else {
obj[list[i]["itemId"]]["count"] += 1;
obj[list[i]["itemId"]]["quantity"] += list[i]["quantity"];
}
}
List sol = [];
for (var key in obj.keys) {
sol.add({"itemId": key, "quantity": obj[key]["quantity"] / obj[key]["count"]});
}
return sol;
}
print(formattedList(data));
}

How to match id with current id in array

How I will check current user id == id and show user like or not in UI
"reference":[
{
"id":"123",
"userid"234"
},
{
"id":"1423",
"userid"25534"
},
{
"id":"15423",
"userid"2335534"
},
]
if this is json response try to convert it in models using https://javiercbk.github.io/json_to_dart/
or here is code that can work for u
var reference = [
{"id": "123", "userid": "234"},
{"id": "1423", "userid": "25534"},
{"id": "15423", "userid": "2335534"},
];
var r = reference.firstWhere((e) => e["id"] == "123");
print(r);
// output
{id: 123, userid: 234}

How to rearrange map keys in dart?

I have a map like below :
{
"Future": [
{
"accountId": 57,
"firstName": "Inez",
"lastName": "Mitchell",
"middleName": "J",
}
],
"Overdue": [
{
"accountId": 5,
"firstName": "Mak",
"lastName": "Mitchell",
"middleName": "M",
}
],
"Due Today": [
{
"accountId": 59,
"firstName": "Jack",
"lastName": "Daniel",
"middleName": "P",
}
]
}
and wanted the map like in below order, Due Today first, Overdue 2nd and Future at last.
{
"Due Today": [
{
"accountId": 59,
"firstName": "Jack",
"lastName": "Daniel",
"middleName": "P",
}
],
"Overdue": [
{
"accountId": 5,
"firstName": "Mak",
"lastName": "Mitchell",
"middleName": "M",
}
],"Future": [
{
"accountId": 57,
"firstName": "Inez",
"lastName": "Mitchell",
"middleName": "J",
}
]
}
also these keys in length are 3 but sometimes we got only two of them means Due Today and Future but we have to make sure order is like 1. Due Today 2. Overdue 3. Future
There is no operation which simply rearranges the iteration order of a normal Dart map.
Dart maps usually default to using LinkedHashMap which orders its element (for example for iteration) in key insertion order.
If a key is already there, changing its value does not change the key's position in the iteration order, but any new key added will end up after all existing keys.
That provides the only avaialble way to change iteration order: Remove the key and add it again, which will put it at the end of the iteration order instead of where it previously was.
So, to reorder a map, the easiest is to create a new map:
var newMap = <String, List<Map<String, String>>>{};
for (var key in ["Due Today", "Overdue", "Future"]) {
if (map.containsKey(key)) newMap[key] = map[key];
}
Then newMap has the keys in the correct order. If you want to update your existing map to the same order, then you can do: map..clear()..addAll(newMap); afterwards.
If you want to avoid the extra map, you can delete keys and re-add them instead.
for (var key in ["Due Today", "Overdue", "Future"]) {
if (map.containsKey(key)) {
var value = map[key];
map.delete(key);
map[key] = value;
}
}
This should remove and re-add each key, if it's there at all, in the order you want them.
var keys = abc.keys.toList()..shuffle();
for(var k in keys) {
print('$k, ${abc[k]}');}
steps you need todo:
convert the map to a temp list
do your edits on the list
clear the map
copy the entries from the list back to map
**
> extension ExtensionsOnMap on Map<String, ChannelItem> {
> void replace(int index, String key, ChannelItem channelItem) {
>
> var tmpLst = this.entries.map((e) => MapEntry(e.key, e.value)).toList();
>
> tmpLst.removeAt(index);
> tmpLst.insert(index, new MapEntry(key, channelItem));
>
> this.clear();
>
> tmpLst.forEach((mapEntry) => this[mapEntry.key] = mapEntry.value);
> }
> }
**
Code:
void main(){
Map map = {
"Future": [
{
"accountId": 57,
"firstName": "Inez",
"lastName": "Mitchell",
"middleName": "J",
}
],
"Overdue": [
{
"accountId": 5,
"firstName": "Mak",
"lastName": "Mitchell",
"middleName": "M",
}
],
"Due Today": [
{
"accountId": 59,
"firstName": "Jack",
"lastName": "Daniel",
"middleName": "P",
}
]
};
for(String key in ['Due Today', 'Overdue', 'Future']){
var value = map[key];
map.remove(key);
map[key] = value;
}
print(map);
}
Output:
{
Due Today: [{
accountId: 59,
firstName: Jack,
lastName: Daniel,
middleName: P
}],
Overdue: [{
accountId: 5,
firstName: Mak,
lastName: Mitchell,
middleName: M
}],
Future: [{
accountId: 57,
firstName: Inez,
lastName: Mitchell,
middleName: J
}]
}