英文:
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:
{
"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.
What is the way to get the latest date from array of objects in flutter?
答案1
得分: 1
您可以使用List.sort
并比较日期。
String jsonData = '''
{
"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"
}...
''';
// 解析JSON数据
Map<String, dynamic> data = jsonDecode(jsonData);
// 按dateofinsp升序对行进行排序
List<Map<String, dynamic>> rows = List<Map<String, dynamic>>.from(data['rows']);
rows.sort((a, b) => DateTime.parse(a['dateofinsp']).compareTo(DateTime.parse(b['dateofinsp'])));
// 打印排序后的行
for (Map<String, dynamic> row in rows) {
print(row);
}
英文:
You can use List.sort
and compare the dates.
String jsonData = '''
{
"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"
}...
''';
// Parse the JSON data
Map<String, dynamic> data = jsonDecode(jsonData);
// Sort the rows by dateofinsp in ascending order
List<Map<String, dynamic>> rows = List<Map<String, dynamic>>.from(data['rows']);
rows.sort((a, b) => DateTime.parse(a['dateofinsp']).compareTo(DateTime.parse(b['dateofinsp'])));
// Print the sorted rows
for (Map<String, dynamic> 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}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论