I have a list of maps as below. I want to get the maps with date two weeks before now.
NOTE (I have reduced the number of maps in the list so that it wont be long but enough to potray the structure of the map),
List<dynamic> allAlerts = [
{
"id": 4037,
"type": "Scouting",
"createdBy": "Algorithm",
"message": "In the firstfield1 section there may be possible pests: Spider mite (Avocado red mite, Oligonychus yothersi), Avocado moth (Avocado Stenoma catenifer), Thrips (Greenhouse thrips, Heliothrips haemorrhoidalis), Persea mite (Oligonychus perseae), Monalonion velezangeli (Monalonion velezangeli), Whitefly (Paraleyrodes sp.), Thrips (Western flower thrips: Frankliniella Occidentalis), Thrips (Avocado thrips, Scirtothrips perseae)",
"date": "2022-08-25T13:49:18.404932",
"fieldId": 206
},
{
"id": 4036,
"type": "Scouting",
"createdBy": "Algorithm",
"message": "In the firstfield1 section there may be possible pests: Spider mite (Avocado red mite, Oligonychus yothersi), Avocado moth (Avocado Stenoma catenifer), Thrips (Greenhouse thrips, Heliothrips haemorrhoidalis), Persea mite (Oligonychus perseae), Monalonion velezangeli (Monalonion velezangeli), Whitefly (Paraleyrodes sp.), Thrips (Western flower thrips: Frankliniella Occidentalis), Thrips (Avocado thrips, Scirtothrips perseae)",
"date": "2022-08-18T13:49:18.404932",
"fieldId": 206
},
{
"id": 4035,
"type": "Scouting",
"createdBy": "Algorithm",
"message": "In the firstfield1 section there may be possible pests: Spider mite (Avocado red mite, Oligonychus yothersi), Avocado moth (Avocado Stenoma catenifer), Thrips (Greenhouse thrips, Heliothrips haemorrhoidalis), Persea mite (Oligonychus perseae), Monalonion velezangeli (Monalonion velezangeli), Whitefly (Paraleyrodes sp.), Thrips (Western flower thrips: Frankliniella Occidentalis), Thrips (Avocado thrips, Scirtothrips perseae)",
"date": "2022-08-12T13:49:18.404932",
"fieldId": 206
},
{
"id": 4034,
"type": "Scouting",
"createdBy": "Algorithm",
"message": "In the firstfield1 section there may be possible pests: Leafminer (Coffee leafminer - Perileucoptera coffeella), Spider mite (Carmine spider mite (Tetranychus cinnabarinus)), Fruit fly - Anastrepha (Anastrepha spp.), Spider mite (Avocado red mite, Oligonychus yothersi)",
"date": "2022-08-25T13:49:18.404932",
"fieldId": 206
},
]
CodePudding user response:
Simply use the methods isAfter(), isBefore() or isAtSameMomentAs() from DateTime.
Other alternative, use compareTo(DateTime other), as in the docs:
Compares this DateTime object to [other], returning zero if the values are equal.
Returns a negative value if this DateTime [isBefore] [other]. It returns 0 if it [isAtSameMomentAs] [other], and returns a positive value otherwise (when this [isAfter] [other]).
Here a code example of sorting dates:
void main() {
var list = [
DateTime.now().add(Duration(const Duration(days: 3))),
DateTime.now().add(Duration(const Duration(days: 2))),
DateTime.now(),
DateTime.now().subtract(Duration(const Duration(days: 1))
];
list.sort((a, b) => a.compareTo(b));
print(list);
}
With subtract, you can get the exact date that you need, for example:
DateTime.now().subtract(Duration(const Duration(days: 14)))
And then, you can compare your dates from list with this one with methods isAfter(), isBefore() or isAtSameMomentAs() from DateTime.
God to know:
There is also Jiffy package that is used for parsing, manipulating, querying and formatting dates.
More info on this link: https://pub.dev/packages/jiffy
CodePudding user response:
Use DateTime.parse
/DateTime.tryParse
to parse the date
entry for each Map
, and filter your list by comparing those DateTime
s to your desired time period.
I want to get the maps with date two weeks before now.
Assuming that you mean that you want all Map
s with a date
value within the past two weeks (and nothing in the future):
var now = DateTime.now();
var twoWeeksAgo = now.subtract(const Duration(days: 14));
var filtered = allAlerts.where((map) {
var date = DateTime.tryParse(map['date'] ?? '');
return date != null &&
date.compareTo(twoWeeksAgo) >= 0 &&
date.compareTo(now) <= 0;
}).toList();
package:basics
provides extensions on DateTime
to make the comparisons more readable (and less error-prone):
return date != null && date >= twoWeeksAgo && date <= now;
CodePudding user response:
You can get the alerts older than two months by filtering the data using where()
filter. Inside the filter, you can check if the date is twoWeeksBefore
or not. If you return true here it means the condition satisfied. Otherwise, the alert is within the two weeks bracket.
final twoWeeksBeforeAlerts = allAlerts.where((e) {
final mapDate = DateTime.tryParse(e['date']);
const duration = Duration(days: 14);
final twoWeeksBefore = DateTime.now().subtract(duration);
return mapDate?.isBefore(twoWeeksBefore) ?? false;
}).toList();