I've been trying to get some messages ordered by date, but I'm not getting it working. I have tried different packages like grouped_list and sticky_headers.
With sticky_headers I managed to get a header above every post but that's not what I want. I want to have the messages sorted by day. Like the image below:
Below I have an dataset with de data I get from my API. My intention is to get 40 messages from the API. Those 40 messages are sorted by time only the messages must be grouped by day in the app as the image above.
Edit: After some playing with sticky_header package on flutter. I have now managed to get the headers. Only do not manage to get the messages under the correct header.
Dataset:
[
{
"time": "2020-06-16T10:31:12.000Z",
"message": "P2 BGM-01 HV buiten materieel (Gas lekkage) Franckstraat Arnhem 073631"
},
{
"time": "2020-06-16T10:29:35.000Z",
"message": "A1 Brahmslaan 3862TD Nijkerk 73278"
},
{
"time": "2020-06-16T10:29:35.000Z",
"message": "A2 NS Station Rheden Dr. Langemijerweg 6991EV Rheden 73286"
},
{
"time": "2020-06-15T09:41:18.000Z",
"message": "A2 VWS Utrechtseweg 6871DR Renkum 74636"
},
{
"time": "2020-06-14T09:40:58.000Z",
"message": "B2 5623EJ : Michelangelolaan Eindhoven Obj: ziekenhuizen 8610 Ca CATH route 522 PAAZ Rit: 66570"
}
]
First, breakdown your code to get group by date map from your dataset. Then use this map to construct the UI.
Use collection package to use groupBy function.
import "package:collection/collection.dart";
void main() {
final dataSet = [
{
"time": "2020-06-16T10:31:12.000Z",
"message": "Message1",
},
{
"time": "2020-06-16T10:29:35.000Z",
"message": "Message2",
},
{
"time": "2020-06-15T09:41:18.000Z",
"message": "Message3",
},
];
var groupByDate = groupBy(dataSet, (obj) => obj['time'].substring(0, 10));
groupByDate.forEach((date, list) {
// Header
print('${date}:');
// Group
list.forEach((listItem) {
// List item
print('${listItem["time"]}, ${listItem["message"]}');
});
// day section divider
print('\n');
});
}
Output:
2020-06-16:
2020-06-16T10:31:12.000Z, Message1
2020-06-16T10:29:35.000Z, Message2
2020-06-15:
2020-06-15T09:41:18.000Z, Message3
I hope this snippet will help you to start constructing your UI. You can use ListView widget for list items in each group.
Go through DateTime, DateFormat classes to handle date, time values.