获取API端对象数组中的最新日期的Flutter方法是什么?

huangapple go评论78阅读模式
英文:

Flutter: How to get the latest date from array of objects in API side?

问题

I use flutter in project.

我在项目中使用Flutter。

I get array of objects from the server. Each object contains few properties and one of them is date property.

我从服务器获取对象数组。每个对象包含一些属性,其中之一是日期属性。

Here is the Array (in json) that I get from server:

这是我从服务器获取的JSON数组:

{
    "drainageid": "",
    "datacount": 98359,
    "resultcount": 98359,
    "page": "",
    "rows": [
        {
            "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
            "dateoflastinsp": null,
            "dateofinsp": "2001-10-10T00:00:00",
            "inspby": "SAW/ZAG"
        },
        {
            "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
            "dateoflastinsp": "2001-10-10T00:00:00",
            "dateofinsp": "2002-09-24T00:00:00",
            "inspby": "SAW+NBN"
        },
        {
            "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
            "dateoflastinsp": "2002-09-24T00:00:00",
            "dateofinsp": "2003-09-18T00:00:00",
            "inspby": "SAW/NBN"
        },
        {
            "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
            "dateoflastinsp": "2003-09-18T00:00:00",
            "dateofinsp": "2004-09-08T00:00:00",
            "inspby": "SAW/NBN"
        },
        {
            "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
            "dateoflastinsp": "2004-09-08T00:00:00",
            "dateofinsp": "2005-05-05T00:00:00",
            "inspby": "MZB/ASMG"
        },
        {
            "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
            "dateoflastinsp": "2005-05-05T00:00:00",
            "dateofinsp": "2006-05-04T00:00:00",
            "inspby": "MZB/ASZ"
        },
        {
            "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
            "dateoflastinsp": "2006-05-04T00:00:00",
            "dateofinsp": "2007-05-09T00:00:00",
            "inspby": "Ahmad/Haizul"
        },
        {
            "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
            "dateoflastinsp": "2007-05-09T00:00:00",
            "dateofinsp": "2008-05-07T00:00:00",
            "inspby": "Harzazi/Zulkornain"
        }
    ]
}

I need to get the latest date dateoflastinsp from the array among the rows list.

我需要从rows列表中获取最新的日期dateoflastinsp

What is the way to get the latest date from array of objects in flutter?

在Flutter中,您可以通过以下方式获取对象数组中的最新日期dateoflastinsp

import 'dart:convert';

// Your JSON data
String jsonData = '...'; // Replace with your JSON data

// Parse the JSON data
Map<String, dynamic> data = json.decode(jsonData);

// Extract the 'rows' list
List<dynamic> rows = data['rows'];

// Sort the list by 'dateoflastinsp' in descending order
rows.sort((a, b) {
  String dateA = a['dateoflastinsp'];
  String dateB = b['dateoflastinsp'];
  if (dateA == null && dateB == null) {
    return 0;
  } else if (dateA == null) {
    return 1;
  } else if (dateB == null) {
    return -1;
  }
  return DateTime.parse(dateB).compareTo(DateTime.parse(dateA));
});

// Get the latest date
String latestDate = rows.first['dateoflastinsp'];

这段代码将帮助您在Flutter中从对象数组中获取最新的日期dateoflastinsp

英文:

I use flutter in project.

I get array of objects from the server. Each object contains few properties and one of them is date property.

Here is the Array (in json) that I get from server:

{
    &quot;drainageid&quot;: &quot;&quot;,
    &quot;datacount&quot;: 98359,
    &quot;resultcount&quot;: 98359,
    &quot;page&quot;: &quot;&quot;,
    &quot;rows&quot;: [
        {
            &quot;id&quot;: &quot;DR/C1/IC/H/-/GPG/PU/-/RCP/01&quot;,
            &quot;dateoflastinsp&quot;: null,
            &quot;dateofinsp&quot;: &quot;2001-10-10T00:00:00&quot;,
            &quot;inspby&quot;: &quot;SAW/ZAG&quot;
        },
        {
            &quot;id&quot;: &quot;DR/C1/IC/H/-/GPG/PU/-/RCP/01&quot;,
            &quot;dateoflastinsp&quot;: &quot;2001-10-10T00:00:00&quot;,
            &quot;dateofinsp&quot;: &quot;2002-09-24T00:00:00&quot;,
            &quot;inspby&quot;: &quot;SAW+NBN&quot;
        },
        {
            &quot;id&quot;: &quot;DR/C1/IC/H/-/GPG/PU/-/RCP/01&quot;,
            &quot;dateoflastinsp&quot;: &quot;2002-09-24T00:00:00&quot;,
            &quot;dateofinsp&quot;: &quot;2003-09-18T00:00:00&quot;,
            &quot;inspby&quot;: &quot;SAW/NBN&quot;
        },
        {
            &quot;id&quot;: &quot;DR/C1/IC/H/-/GPG/PU/-/RCP/01&quot;,
            &quot;dateoflastinsp&quot;: &quot;2003-09-18T00:00:00&quot;,
            &quot;dateofinsp&quot;: &quot;2004-09-08T00:00:00&quot;,
            &quot;inspby&quot;: &quot;SAW/NBN&quot;
        },
        {
            &quot;id&quot;: &quot;DR/C1/IC/H/-/GPG/PU/-/RCP/01&quot;,
            &quot;dateoflastinsp&quot;: &quot;2004-09-08T00:00:00&quot;,
            &quot;dateofinsp&quot;: &quot;2005-05-05T00:00:00&quot;,
            &quot;inspby&quot;: &quot;MZB/ASMG&quot;
        },
        {
            &quot;id&quot;: &quot;DR/C1/IC/H/-/GPG/PU/-/RCP/01&quot;,
            &quot;dateoflastinsp&quot;: &quot;2005-05-05T00:00:00&quot;,
            &quot;dateofinsp&quot;: &quot;2006-05-04T00:00:00&quot;,
            &quot;inspby&quot;: &quot;MZB/ASZ&quot;
        },
        {
            &quot;id&quot;: &quot;DR/C1/IC/H/-/GPG/PU/-/RCP/01&quot;,
            &quot;dateoflastinsp&quot;: &quot;2006-05-04T00:00:00&quot;,
            &quot;dateofinsp&quot;: &quot;2007-05-09T00:00:00&quot;,
            &quot;inspby&quot;: &quot;Ahmad/Haizul&quot;
        },
        {
            &quot;id&quot;: &quot;DR/C1/IC/H/-/GPG/PU/-/RCP/01&quot;,
            &quot;dateoflastinsp&quot;: &quot;2007-05-09T00:00:00&quot;,
            &quot;dateofinsp&quot;: &quot;2008-05-07T00:00:00&quot;,
            &quot;inspby&quot;: &quot;Harzazi/Zulkornain&quot;
        },
}

I need to get the latest date dateoflastinsp from the array among the rows list.

What is the way to get the latest date from array of objects in flutter?

答案1

得分: 1

您可以使用List.sort并比较日期。


String jsonData = &#39;&#39;&#39;
  {
    &quot;drainageid&quot;: &quot;&quot;,
    &quot;datacount&quot;: 98359,
    &quot;resultcount&quot;: 98359,
    &quot;page&quot;: &quot;&quot;,
    &quot;rows&quot;: [
      {
        &quot;id&quot;: &quot;DR/C1/IC/H/-/GPG/PU/-/RCP/01&quot;,
        &quot;dateoflastinsp&quot;: null,
        &quot;dateofinsp&quot;: &quot;2001-10-10T00:00:00&quot;,
        &quot;inspby&quot;: &quot;SAW/ZAG&quot;
      }...
 &#39;&#39;&#39;;


// 解析JSON数据
  Map&lt;String, dynamic&gt; data = jsonDecode(jsonData);

  // 按dateofinsp升序对行进行排序
  List&lt;Map&lt;String, dynamic&gt;&gt; rows = List&lt;Map&lt;String, dynamic&gt;&gt;.from(data[&#39;rows&#39;]);
  rows.sort((a, b) =&gt; DateTime.parse(a[&#39;dateofinsp&#39;]).compareTo(DateTime.parse(b[&#39;dateofinsp&#39;])));

  // 打印排序后的行
  for (Map&lt;String, dynamic&gt; row in rows) {
    print(row);
  }

英文:

You can use List.sort and compare the dates.


String jsonData = &#39;&#39;&#39;
  {
    &quot;drainageid&quot;: &quot;&quot;,
    &quot;datacount&quot;: 98359,
    &quot;resultcount&quot;: 98359,
    &quot;page&quot;: &quot;&quot;,
    &quot;rows&quot;: [
      {
        &quot;id&quot;: &quot;DR/C1/IC/H/-/GPG/PU/-/RCP/01&quot;,
        &quot;dateoflastinsp&quot;: null,
        &quot;dateofinsp&quot;: &quot;2001-10-10T00:00:00&quot;,
        &quot;inspby&quot;: &quot;SAW/ZAG&quot;
      }...
 &#39;&#39;&#39;;


// Parse the JSON data
  Map&lt;String, dynamic&gt; data = jsonDecode(jsonData);

  // Sort the rows by dateofinsp in ascending order
  List&lt;Map&lt;String, dynamic&gt;&gt; rows = List&lt;Map&lt;String, dynamic&gt;&gt;.from(data[&#39;rows&#39;]);
  rows.sort((a, b) =&gt; DateTime.parse(a[&#39;dateofinsp&#39;]).compareTo(DateTime.parse(b[&#39;dateofinsp&#39;])));

  // Print the sorted rows
  for (Map&lt;String, dynamic&gt; row in rows) {
    print(row);
  }

output

{id: DR/C1/IC/H/-/GPG/PU/-/RCP/01, dateoflastinsp: null, dateofinsp: 2001-10-10T00:00:00, inspby: SAW/ZAG}
{id: DR/C1/IC/H/-/GPG/PU/-/RCP/01, dateoflastinsp: 2001-10-10T00:00:00, dateofinsp: 2002-09-24T00:00:00, inspby: SAW+NBN}
{id: DR/C1/IC/H/-/GPG/PU/-/RCP/01, dateoflastinsp: 2002-09-24T00:00:00, dateofinsp: 2003-09-18T00:00:00, inspby: SAW/NBN}
{id: DR/C1/IC/H/-/GPG/PU/-/RCP/01, dateoflastinsp: 2003-09-18T00:00:00, dateofinsp: 2004-09-08T00:00:00, inspby: SAW/NBN}
{id: DR/C1/IC/H/-/GPG/PU/-/RCP/01, dateoflastinsp: 2004-09-08T00:00:00, dateofinsp: 2005-05-05T00:00:00, inspby: MZB/ASMG}
{id: DR/C1/IC/H/-/GPG/PU/-/RCP/01, dateoflastinsp: 2005-05-05T00:00:00, dateofinsp: 2006-05-04T00:00:00, inspby: MZB/ASZ}
{id: DR/C1/IC/H/-/GPG/PU/-/RCP/01, dateoflastinsp: 2006-05-04T00:00:00, dateofinsp: 2007-05-09T00:00:00, inspby: Ahmad/Haizul}
{id: DR/C1/IC/H/-/GPG/PU/-/RCP/01, dateoflastinsp: 2007-05-09T00:00:00, dateofinsp: 2008-05-07T00:00:00, inspby: Harzazi/Zulkornain}

huangapple
  • 本文由 发表于 2023年6月12日 13:39:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定