dart flutter max value from list of objects

aknd

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},
  ...
];
Pablo Barrera

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));
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Dart Built Value Deserialize List of Objects

分類Dev

Parsing a List in Flutter / Dart

分類Dev

Flutter Dart Search List

分類Dev

Max value within array of objects

分類Dev

Dart/Flutter How to initialize a List inside a Map?

分類Dev

How to compare two list data in flutter (dart)

分類Dev

Loading a file from the assets folder in Flutter / Dart

分類Dev

Plotting objects from a list

分類Dev

Max value of integer from nested irregular list (distinct sublists length and object types) in Python

分類Dev

Convert a UInt8List to Image in flutter/dart-ui

分類Dev

Is it possible to create a List in Flutter/Dart with more than one type?

分類Dev

React remove object from list of objects without unique value, using hooks

分類Dev

Get ComboBox selected value from a list which has two types of objects and the combobox is only showing one of them

分類Dev

remove Objects from a List base on another list

分類Dev

How to convert From DateTime to unix timestamp in Flutter or Dart in general

分類Dev

Selecting MAX on column then MAX from column that is dependent on first value

分類Dev

SQL Query for max value from subsets

分類Dev

How to get max value from a json?

分類Dev

php get max key from same value

分類Dev

Selecting a max value from a pivot table

分類Dev

Mysql - Return max value from joined table

分類Dev

Get another value from row with max value with group by clause

分類Dev

Java - Remove null from list of Objects

分類Dev

get list of unique objects from an arraylist in java

分類Dev

Creating a file of json objects from a list of dicts

分類Dev

How to select only not null objects from list

分類Dev

Flutter Dart constructor

分類Dev

Flutter Dart constructor

分類Dev

Async / Await / then in Dart / Flutter

Related 関連記事

  1. 1

    Dart Built Value Deserialize List of Objects

  2. 2

    Parsing a List in Flutter / Dart

  3. 3

    Flutter Dart Search List

  4. 4

    Max value within array of objects

  5. 5

    Dart/Flutter How to initialize a List inside a Map?

  6. 6

    How to compare two list data in flutter (dart)

  7. 7

    Loading a file from the assets folder in Flutter / Dart

  8. 8

    Plotting objects from a list

  9. 9

    Max value of integer from nested irregular list (distinct sublists length and object types) in Python

  10. 10

    Convert a UInt8List to Image in flutter/dart-ui

  11. 11

    Is it possible to create a List in Flutter/Dart with more than one type?

  12. 12

    React remove object from list of objects without unique value, using hooks

  13. 13

    Get ComboBox selected value from a list which has two types of objects and the combobox is only showing one of them

  14. 14

    remove Objects from a List base on another list

  15. 15

    How to convert From DateTime to unix timestamp in Flutter or Dart in general

  16. 16

    Selecting MAX on column then MAX from column that is dependent on first value

  17. 17

    SQL Query for max value from subsets

  18. 18

    How to get max value from a json?

  19. 19

    php get max key from same value

  20. 20

    Selecting a max value from a pivot table

  21. 21

    Mysql - Return max value from joined table

  22. 22

    Get another value from row with max value with group by clause

  23. 23

    Java - Remove null from list of Objects

  24. 24

    get list of unique objects from an arraylist in java

  25. 25

    Creating a file of json objects from a list of dicts

  26. 26

    How to select only not null objects from list

  27. 27

    Flutter Dart constructor

  28. 28

    Flutter Dart constructor

  29. 29

    Async / Await / then in Dart / Flutter

ホットタグ

アーカイブ